问题
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:
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: (inside the tableView wish will show the cell)
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
}
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?
回答1:
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
Hope this helps
Daniel
来源:https://stackoverflow.com/questions/35161109/ios-where-to-put-custom-cell-design-awakefromnib-or-cellforrowatindexpath