闭包修改cell的内容,防止引用循环

2019-01-25  本文已影响4人  落夏简叶

代码基于: Swift 4.0

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XXTestCell", for: indexPath) as! XXTestCell
        // 防止引用循环
        // 1. 重新获取一个局部cell
//        cell.buttonCallBack = {
//            let cell = tableView.cellForRow(at: indexPath) as? XXTestCell
//            cell?.configureLabel(with: "110")
//        }
        
        // 2.不用重新获取,[weak cell]方式。以前没用过,用了之后发现很好用(推荐这种)
        cell.buttonCallBack = { [weak cell] in
            cell?.configureLabel(with: "110")
        }
        
        return cell
    }
上一篇 下一篇

猜你喜欢

热点阅读