swift中cell重用的一些写法

2018-07-11  本文已影响11人  lanmoyingsheng
// 注册写法
public class func reuseCellWith(tableView:UITableView, indexPath:IndexPath) -> NewsListCell {

    // 注册写法
    let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(self.self), for:indexPath) as! NewsListCell

    return cell
}
// 非注册写法
public class func reuseCellWith(tableView:UITableView) -> NewsListCell {

    // 非注册写法
    guard let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(self.self)), let checkCell = (cell as? NewsListCell)  else {
        return NewsListCell(style: .default, reuseIdentifier: NSStringFromClass(self.self))
    }
    return checkCell
}

初步验证后,发现内存占用差不多。所以建议使用第一种,代码量更少,只不过需要为tableview注册cell而已。

上一篇 下一篇

猜你喜欢

热点阅读