美文网首页
swift中cell重用的一些写法

swift中cell重用的一些写法

作者: lanmoyingsheng | 来源:发表于2018-07-11 11:23 被阅读11次
    // 注册写法
    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而已。

    相关文章

      网友评论

          本文标题:swift中cell重用的一些写法

          本文链接:https://www.haomeiwen.com/subject/jdxhpftx.html