微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义委托导致按钮点击错误

如何解决自定义委托导致按钮点击错误

我正在使用 MVVM 架构在 swift 上开发一个简单的 tableView 项目。但是当我点击那个 tableView 上的按钮时,我收到了这样的错误

线程1:同时访问0x7faf490331c8,但修改需要独占访问

我已经定义了一个这样的模型

struct Person{
    var name: String
    var username : String
    var currentFollowing : Bool
    let image : UIImage?
}

一个自定义的委托协议

protocol  PersonFollowingTableViewCellDelegate : AnyObject {
    
    func PersonFollowingTableViewCell( _ cell: PersonFollowingTableViewCell,didTapWith  : Person)
}

我正在尝试使用它来配置按钮

 @objc private func didTapButton(){
        let defaultPerson = Person(name: "default",username: "default",currentFollowing: true,image: nil)
        person?.currentFollowing = !(person?.currentFollowing ?? true)
        delegate?.PersonFollowingTableViewCell(self,didTapWith: person ?? defaultPerson )
        prepareForReuse()
        configure(with: person ?? defaultPerson)
    }

ViewController 中的回调方法在这里

extension ViewController : PersonFollowingTableViewCellDelegate{
    func PersonFollowingTableViewCell(_ cell: PersonFollowingTableViewCell,didTapWith array: Person) {
        return
    }

解决方法

修改你的@objc 私有函数 didTapButton()。 而不是

person?.currentFollowing = !(person?.currentFollowing ?? true)

制作

let currentFollowing = !(person?.currentFollowing ?? true)
person?.currentFollowing = currentFollowing

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。