鍍金池/ 問答/iOS/ 使用swift創(chuàng)建tableview中cell無法復用,大家?guī)兔纯?/span>

使用swift創(chuàng)建tableview中cell無法復用,大家?guī)兔纯?/h1>

我用swift自定義了一個cell,同時復寫了init方法,但是每回調(diào)用的時候,都不走

if cell.isEqual(nil) {

            cell = WYLClockDetailCell.init(style: .default, reuseIdentifier: "cell", tag:indexPath.row)

}

原因是每回在dequeueReusableCell的時候,就莫名的調(diào)用了

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

}

而我真正其實是希望進入自己復寫的init方法,如下

init(style: UITableViewCellStyle, reuseIdentifier: String?, tag: Int) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.backgroundColor = UIColor.white
        upInfoCell()

}

我的完整代碼:
tableViewCell

class WYLClockDetailCell: UITableViewCell {

    var cellTitle: UILabel?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        //每次都進入這個
        super.init(style: style, reuseIdentifier: reuseIdentifier)

    }
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, tag: Int) {
        //而我希望進入這個方法
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.backgroundColor = UIColor.white
        upInfoCell()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

tableview的創(chuàng)建方法:

override func viewDidLoad() {
        
        super.viewDidLoad()

        dataArray = ["內(nèi)容","日期","時間","是否提前","重復","鈴聲","細節(jié)"]
        
        let tableView:UITableView = UITableView.init()
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = UIColor.init(red: CGFloat.init(arc4random()%255)/255.0, green: CGFloat.init(arc4random()%255)/255.0, blue: CGFloat.init(arc4random()%255)/255.0, alpha: 1)
        tableView.bounces = false
        tableView.separatorStyle = .none;
        self.view.addSubview(tableView)
        
        tableView.register(WYLClockDetailCell.self, forCellReuseIdentifier: "cell")
        
        weak var weakSelf = self
        
        tableView.snp.makeConstraints { (maker) in
        
            maker.bottom.equalTo(weakSelf!.view).offset(-kRealValue()*5.5)
            maker.left.equalTo(weakSelf!.view).offset(0)
            maker.right.equalTo(weakSelf!.view).offset(0)
            maker.height.equalTo(46*dataArray!.count)
            
        }
        
        self.view.backgroundColor = UIColor.init(red: CGFloat.init(arc4random()%255)/255.0, green: CGFloat.init(arc4random()%255)/255.0, blue: CGFloat.init(arc4random()%255)/255.0, alpha: 1)
        
        tableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell:WYLClockDetailCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WYLClockDetailCell
        
        if cell.isEqual(nil) {

            cell = WYLClockDetailCell.init(style: .default, reuseIdentifier: "cell", tag:indexPath.row)

        }
        
        return cell
        
}

還有一個問題是整個tableview都無法復用了,每次都會直接從dequeueReusableCell直接生成一個cell,請問哪里出了問題,怎么解決呢

回答
編輯回答
心沉

去掉register,因為register會直接init一個cell出來

tableView.register(WYLClockDetailCell.self, forCellReuseIdentifier: "cell")

要用 ? 而不是 ! 因為!會直接強制出一個cell

var cell = tableView.dequeueReusableCell(withIdentifier:idf) as? WYLClockDetailCell
        
        if cell == nil {
            cell = WYLClockDetailCell.init(style: .default, reuseIdentifier: idf, tag: indexPath.row)
        }
2017年11月22日 13:28