swift4.0 cell重用导致ReactiveCocoa(R
2020-02-20 本文已影响0人
彧哥哥
问题描述
在UITableview等涉及cell的重用的界面中,cell上的按钮点击事件RAC响应会随着cell的重用多次触发,并导致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对象中触发按钮的RAC点击事件
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
在不滚动界面的情况下点价按钮只响应了一次
========voice button
滚动界面后点击按钮,直接打印了四个log
========voice button
========voice button
========voice button
========voice button
解决办法
这个问题是由于cell重用导致的,需要解除signal在重用时的绑定。
cell.voiceBtn.reactive.controlEvents(.touchUpInside).take(until: cell.reactive.prepareForReuse) .observeValues { (sender) in
print("========voice button")
}
采用take(until: cell.reactive.prepareForReuse)解除cell重用时signal的绑定
滚动界面触发按钮事件查看log
========voice button
嘿嘿,只有一次啦,成功解决