美文网首页集思广益
swift4.0 cell重用导致ReactiveCocoa(R

swift4.0 cell重用导致ReactiveCocoa(R

作者: 钻石筷子 | 来源:发表于2018-03-20 04:38 被阅读0次

swift4.0 cell重用导致ReactiveCocoa(RAC) cell上Button点击事件多次触发问题

问题描述

在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

嘿嘿,只有一次啦,成功解决

相关文章

网友评论

    本文标题:swift4.0 cell重用导致ReactiveCocoa(R

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