iOS在哪里放置自定义单元格设计?awakeFromNib 或 cellForRowAtIndexPath? [英] iOS where to put custom cell design? awakeFromNib or cellForRowAtIndexPath?

查看:55
本文介绍了iOS在哪里放置自定义单元格设计?awakeFromNib 或 cellForRowAtIndexPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, basically i made a custom cell from a nib, wish i apply a little custom design, like colors and shadows.

我找到了两种应用样式的方法:

I found two ways of applying the styling:

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() 中,设计只执行一次,所以这是更好的吗?

In terms of performance, wish one will be better? I've noticed that in a awakeFromNib() the design only does it once, so this is the better one?

推荐答案

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

As you mentioned awakeFromNib is only called once (when the cell is instantiated), if its setting stuff like background colors and stuff like that that wont change then its ok to do it there, cell customization (the data that the cell is showing) should be done during cellForRowAtIndexPath, so you should not check if the data is empty there rather, give it the data everytime the cell is returned, this will allow you to reuse cells and set the data as needed

希望能帮到你

丹尼尔

这篇关于iOS在哪里放置自定义单元格设计?awakeFromNib 或 cellForRowAtIndexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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