UITableViewCell比其UITableView宽-向右延伸? [英] UITableViewCell is wider than its UITableView - extending to the right?

查看:68
本文介绍了UITableViewCell比其UITableView宽-向右延伸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个带有单个单元格的UITableView.单元内是UILabel.名为PickerTableViewCell的单元格从xib获取其UI,并将其添加到其子视图中.这样做是因为许多不同的自定义单元都使用了该xib.

I have a UITableView with a single cell. Inside the cell is a UILabel. The cell, called PickerTableViewCell, gets its UI from a xib and adds it to its subview. This is done because this xib is used by many different custom cells.

问题

当此单元格由表格视图出队时,它的宽度超出了右侧的表格视图宽度.

It appears that when this cell is dequeued by the table view, its width extends beyond the table views width on the right hand side.

此处是UILabel居中放置的xib,然后是它在设备上的显示方式.请注意标签在设备上的偏心方式:

Here is the xib with the UILabel centred within the cell, followed by how it appears on the device. Notice how the label is off-centre on the device:

当我将标签移到单元格(label.trailing = pickerCell.trailing + 0)的最右侧时,它会从右侧消失.以下是xib在xib中的外观以及在设备上的外观:

When I move the label to the very right of the cell (label.trailing = pickerCell.trailing + 0), it disappears off the right hand side. Below is how that looks in the xib followed by how it appears on the device:

当我将UILabel对准单元格(label.leading = pickerCell.leading + 0)的左侧时,它正确地对准了设备的左侧.

When I align the UILabel to the left of the cell (label.leading = pickerCell.leading + 0), it correctly aligns to the left on the device.

这是PickerTableViewCell加载xib的方式:

Here is how the PickerTableViewCell loads the xib:

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)


        // load the xib to associate with
        let nib = Bundle.main.loadNibNamed("PickerTableViewCell", owner: nil, options: nil)
        if let view = nib?.first as? UIView{
            self.addSubview(view)
        }
    // ...
}

这是表格视图注册单元格的方式:

Here is how the tableview registers the cell:

self.tableView.register(PickerTableViewCell.self, forCellReuseIdentifier: "pickerCell")

最后是如何将其出队:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "pickerCell") as! PickerTableViewCell
        return cell
}

任何人都可以帮助您了解发生了什么吗?为什么我的单元格似乎超出了表格视图的宽度?

Could anyone help understand what is going on?? Why is my cell seeming to extend beyond the width of the table view?

推荐答案

分配任何xib时,必须指定frame.这是修改后的代码:

While assigning any xib, you have to specify the frame. Here is the modified code:

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    // load the xib to associate with
    let nib = Bundle.main.loadNibNamed("PickerTableViewCell", owner: nil, options: nil)
    if let view = nib?.first as? UIView{
        self.addSubview(view)
        view.frame = bounds //Provide the frame
    }
    // ...
}

这篇关于UITableViewCell比其UITableView宽-向右延伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆