【问题标题】:Custom UITableViewCell not appearing自定义 UITableViewCell 未出现
【发布时间】:2016-06-11 04:10:04
【问题描述】:

我有一个带有自定义单元格视图的UITableViewController。我创建了一个空的 Interface Builder 文档,然后添加了一个 Table View Cell,然后为其添加了一个标签。 Table View Cell 有一个对应的类扩展UITableViewCell。 Interface Builder 中的 Table View Cell 的标签链接(outet)到我的自定义类中的 var

class MyTableViewCell: UITableViewCell {
    @IBOutlet var someLabel: UILabel!

问题是自定义单元格永远不会呈现,它总是空白(我也尝试了背景颜色技巧)。我从来没有看到标签。实际上标签始终为空。

在我的UITableViewControllerviewDidLoad() 中,我试过了

let nib = UINib(nibName: "MyTableCellView", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "myCell")

还有

tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myCell")

我也有

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell
    print("cellForRowAtIndexPath, cell = \(cell), someLabel = \(cell.someLabel)")
    return cell
}

在运行时它正在出队,因为cell 不为空,但cell.someLabel 为空。

自定义表格视图单元格渲染需要什么?

【问题讨论】:

  • 这里有同样的问题,我可以点击单元格,但它不呈现。你最后解决了吗?

标签: ios swift uitableview


【解决方案1】:

someLabel 没有价值。试试:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell
    cell.someLabel.text = "Put label text here"
    return cell
}

【讨论】:

    【解决方案2】:

    我通常这样做。我在自定义表格视图单元类中加载 xib 文件。

    class MyTableViewCell: UITableViewCell {
    
        @IBOutlet weak var label: UILabel!
    
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            xibSetup()
        }
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)!
            xibSetup()
        }
    
        func xibSetup() {
            cell = loadViewFromNib()
            cell.frame = self.bounds
            cell.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
            addSubview(cell)
        }
    
        func loadViewFromNib() -> UITableViewCell {
            let bundle = NSBundle(forClass: self.dynamicType)
            let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)
            let cell = nib.instantiateWithOwner(self, options: nil)[0] as! UITableViewCell
            return cell
        }
    
    }
    

    随着:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell
        print("cellForRowAtIndexPath, cell = \(cell), someLabel = \(cell.someLabel)")
        return cell
    }
    

    另一件事是将 MyTableViewCell.xib 文件中的 File's Owner 设置为 MyTableViewCell 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多