Кнопка «Запретить» в повторно используемых ячейках, чтобы появиться снова - PullRequest
0 голосов
/ 27 мая 2018
 @objc func addinterestserver(_ sender: UIButton) {

                                        sender.backgroundColor = UIColor.white
                                        sender.setTitleColor(UIColor.black, for: .normal)
                                        sender.setTitle(NSLocalizedString("Added", comment: ""), for: .normal)
                                        sender.isEnabled = false
}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SearchInterestsCustom

        let release = arrayOfRels[(indexPath as NSIndexPath).row]
        cell.user.text = release.title
        cell.match.text = release.count + NSLocalizedString(" people added", comment: "")
        cell.addbutton.tag = release.eventID
        cell.addbutton.addTarget(self, action: #selector(SearchInterests.addinterestserver), for: .touchUpInside)
        cell.addbutton.layer.cornerRadius = 20
        return cell
    }

После того, как пользователь нажал кнопку, он влияет и на кнопку другой ячейки, ясно, что я вижу одни и те же измененные кнопки несколько раз при прокрутке.

Как этого избежать?

1 Ответ

0 голосов
/ 27 мая 2018

Вы можете избежать этого с помощью

var allSelected = [Int]()

//

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SearchInterestsCustom

   if allSelected.contains(release.eventID) {
       cell.addbutton.backgroundColor = UIColor.white
       cell.addbutton.setTitleColor(UIColor.black, for: .normal)
       cell.addbutton.setTitle(NSLocalizedString("Added", comment: ""), for: .normal)
       cell.addbutton.isEnabled = false
   }
   else {
         // set default values 


   }

 }

//

 @objc func addinterestserver(_ sender: UIButton) {

       sender.backgroundColor = UIColor.white
       sender.setTitleColor(UIColor.black, for: .normal)
       sender.setTitle(NSLocalizedString("Added", comment: ""), for: .normal)   
       sender.isEnabled = false

       allSelected.append(sender.tag) // store it here 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...