防止 tableview 被重用 (MVVM)

问题描述

我知道如何在来回滚动后保留我们在 UITableView 上所做的操作。

现在我正在 MVVM 上做一个简单的 UITableView 它有一个“关注”按钮 .

follow button

像这样。 点击后关注按钮变为取消关注,滚动后重置。 在哪里以及如何添加代码来防止这种情况发生?

这是tableview代码

func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return Vm.personFollowingTableViewViewModel.count
    
}
var selectedIndexArray:[Int] = []
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    guard let cell = tableView.dequeueReusableCell(withIdentifier: FollowList_MVVM.PersonFollowingTableViewCell.identifier,for: indexPath) as? PersonFollowingTableViewCell else{
        return UITableViewCell()
    }


    cell.configure(with: Vm.personFollowingTableViewViewModel[indexPath.row])
    cell.delegate = self
    return cell
    
    
}

configure(with: )函数

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


func configure(with person1 : Person){
    
    
    self.person = person1
    nameLabel.text = person1.name
    usernameLabel.text = person1.username
    userImageview.image = person1.image
    
    
    
    if person1.currentFollowing{
        //Code to change button UI
        
    }

使用了 Person 类型的自定义委托

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)