【问题标题】:Custom Delegate causing errors on button tap自定义委托导致按钮点击错误
【发布时间】:2021-08-28 13:25:33
【问题描述】:

我正在使用 MVVM 架构快速开发一个简单的 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
    }

【问题讨论】:

  • 你在prepareForReuse()中添加了什么吗?
  • 是的。将所有值初始化为 nil
  • 尝试对此发表评论并检查
  • 还是一样的错误
  • 此错误表明存在线程问题。您是否有任何代码在后台线程/GCD 队列上进行处理?

标签: ios swift uitableview mvvm delegates


【解决方案1】:

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

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

制作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    相关资源
    最近更新 更多