Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义UITableViewCell未显示_Ios_Swift_Uitableview - Fatal编程技术网

Ios 自定义UITableViewCell未显示

Ios 自定义UITableViewCell未显示,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个带有自定义单元格视图的UITableViewController。我创建了一个空的Interface Builder文档,然后添加了一个表视图单元格,然后向其中添加了一个标签。表视图单元格有一个相应的类,该类扩展了UITableViewCell。Interface Builder中的表视图单元格标签链接(outet)到自定义类中的var class MyTableViewCell: UITableViewCell { @IBOutlet var someLabel: UILabe

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

class MyTableViewCell: UITableViewCell {
    @IBOutlet var someLabel: UILabel!
问题是自定义单元格从不渲染,它总是空白的(我也尝试过背景色技巧)。我从来没有看到过标签。事实上,标签总是空的

在我的
UITableViewController
viewDidLoad()
中,我尝试了

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
为非null,但是
cell.someLabel
为nil


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

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
}

我通常这样做。我在自定义表格视图单元类中加载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文件中的文件所有者设置为MyTableViewCell类。

同样的问题,我可以单击单元格,但它不会渲染。你最终解决了吗?
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
    }

}