swift4.0 cell重用導(dǎo)致ReactiveCocoa(RAC) cell上Button點擊事件多次觸發(fā)問題

swift4.0 cell重用導(dǎo)致ReactiveCocoa(RAC) cell上Button點擊事件多次觸發(fā)問題

問題描述

在UITableview等涉及cell的重用的界面中,cell上的按鈕點擊事件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點擊事件

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

在不滾動界面的情況下點價按鈕只響應(yīng)了一次

========voice button

滾動界面后點擊按鈕,直接打印了四個log

========voice button
========voice button
========voice button
========voice button

解決辦法

這個問題是由于cell重用導(dǎo)致的,需要解除signal在重用時的綁定。

cell.voiceBtn.reactive.controlEvents(.touchUpInside).take(until: cell.reactive.prepareForReuse) .observeValues { (sender) in
                print("========voice button")
            }
        
采用take(until: cell.reactive.prepareForReuse)解除cell重用時signal的綁定 

滾動界面觸發(fā)按鈕事件查看log

========voice button

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容