swift4.0 cell重用導(dǎo)致ReactiveCocoa(RAC) cell上Button點(diǎn)擊事件多次觸發(fā)問題
問題描述
在UITableview等涉及cell的重用的界面中,cell上的按鈕點(diǎn)擊事件RAC響應(yīng)會隨著cell的重用多次觸發(fā),并導(dǎo)致cell無法釋放
cell上添加按鈕
lazy var voiceBtn: UIButton = {
let button = UIButton()
button.setImage(UIImage.init(named: "chat_speek3_voiceR"), for: UIControlState.normal)
button.setImage(UIImage.init(named: "chat_speek3_voiceR"), for: UIControlState.highlighted)
requestIM.addSubview(button)
return button
}()
在cell對象中觸發(fā)按鈕的RAC點(diǎn)擊事件
guard let cell = tableView.dequeueReusableCell(withIdentifier: "UTBVChatCell", for: indexPath) as? UTBVChatCell else {
return UITableViewCell()
}
cell.voiceBtn.reactive.controlEvents(.touchUpInside).observeValues { (sender) in
print("========voice button")
}
return cell
在不滾動界面的情況下點(diǎn)價(jià)按鈕只響應(yīng)了一次
========voice button
滾動界面后點(diǎn)擊按鈕,直接打印了四個(gè)log
========voice button
========voice button
========voice button
========voice button
解決辦法
這個(gè)問題是由于cell重用導(dǎo)致的饲漾,需要解除signal在重用時(shí)的綁定吃媒。
cell.voiceBtn.reactive.controlEvents(.touchUpInside).take(until: cell.reactive.prepareForReuse) .observeValues { (sender) in
print("========voice button")
}
采用take(until: cell.reactive.prepareForReuse)解除cell重用時(shí)signal的綁定
滾動界面觸發(fā)按鈕事件查看log
========voice button
嘿嘿逐工,只有一次啦凶朗,成功解決