【问题标题】:iOS where to put custom cell design? awakeFromNib or cellForRowAtIndexPath?iOS 在哪里放置自定义单元格设计? awakeFromNib 还是 cellForRowAtIndexPath?
【发布时间】:2016-02-02 18:25:13
【问题描述】:

所以,基本上我用笔尖制作了一个自定义单元,希望我应用一些自定义设计,比如颜色和阴影。

我发现了两种应用样式的方法:

awakeFromNib():

override func awakeFromNib() {
    super.awakeFromNib()

    //Container Card Style
    self.container.layer.cornerRadius = 3
    self.container.setDropShadow(UIColor.blackColor(), opacity: 0.20, xOffset: 1.5, yOffset: 2.0, radius: 1.8)

    //Rounded thumbnail
    self.thumb_image.setRoundedShape()
    self.thumb_image.backgroundColor = UIColor.customGreyTableBackground()

    //Cell
    self.backgroundColor = UIColor.customGreyTableBackground()
    self.selectionStyle = .None
}

cellForRowAtIndexPath:(tableView里面希望会显示单元格)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //get cell type
    let cell = tableView.dequeueReusableCellWithIdentifier("searchResultCell", forIndexPath: indexPath) as! SearchResultCell

    //Container Card Style
    cell.container.layer.cornerRadius = 3
    cell.container.setDropShadow(UIColor.blackColor(), opacity: 0.20, xOffset: 1.5, yOffset: 2.0, radius: 1.8)

    //Rounded thumbnail
    cell.thumb_image.setRoundedShape()
    cell.thumb_image.backgroundColor = UIColor.customGreyTableBackground()

    //Cell
    cell.backgroundColor = UIColor.customGreyTableBackground()
    cell.selectionStyle = .None

    //cell data
    if(!data.isEmpty){
        cell.name_label.text = data[indexPath.row].name
        cell.thumb_url = data[indexPath.row].thumb_url
    }

    return cell
}

在性能方面,希望一个会更好吗?我注意到在 awakeFromNib() 设计中只执行一次,所以这是更好的一个?

【问题讨论】:

    标签: ios swift performance uitableview custom-cell


    【解决方案1】:

    正如您提到的,awakeFromNib 只被调用一次(当单元格被实例化时),如果它的设置诸如背景颜色之类的东西不会改变,那么它可以在那里进行,单元格自定义(单元格的数据是显示)应该在 cellForRowAtIndexPath 期间完成,因此您不应该检查那里的数据是否为空,而是在每次返回单元格时给它数据,这将允许您重用单元格并根据需要设置数据

    希望对你有帮助

    丹尼尔

    【讨论】:

    • 谢谢丹尼尔,我认为这是正确的想法,但是由于人们说将数据放在 cellForRowAtIndexPath 中,我想为什么不设计..所以在这里提出这个问题是为了清除一切我的想法:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    相关资源
    最近更新 更多