0

всем привет. Есть ячейка в tableview, на которой лежат label и кнопка. Мне надо по нажатию на кнопку менять саму кнопку, а по нажатию на ячейку - переходить на другой view. Переход на view делаю через didSelectRow. Подскажите, какие есть варианты работы с ячейкой, чтобы осуществить этот функционал?

2
  • Вариантов много. Можно на кнопку повесить делегат и обрабатывать его. Или посмотрите вот здесь, например: ru.stackoverflow.com/questions/1232212/… 4 фев 2021 в 13:34
  • Если кнопка написана кодом, то приделать к ней addTarget(...) с указанием на метод, который будет менять кнопку. Если кнопка не кодом, а на сториборде, то выводите Action и в нем меняете вид кнопки. Про ячейку у вас и так все решено, по тапу переходите через present(...)
    – NSA-bot
    5 фев 2021 в 7:57

1 ответ 1

0

спасибо всем за помощь. Сделала так:

в подклассе Cell прописала:

    var buttonTap: () -> ()  = { }

     let checkButton: UIButton = {
           let button = UIButton()
            button.translatesAutoresizingMaskIntoConstraints = false
            return button
        }()
        
        @objc func didTapCheckButton() {
           buttonTap()
        }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
    
            contentView.addSubview(checkButton)
            checkButton.addTarget(self, action: #selector(didTapCheckButton), for: .touchUpInside)
            
        }
 public func refresh(_ model: TaskModel) {
      
        switch model.isComleted {
        case true:
           checkButton.setImage(UIImage(named: "check"), for: .normal)
        case false:
            checkButton.setImage(UIImage(named: "circle"), for: .normal)
            
        } 
      
    }

В подклассе TableView:
.....
 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as? TaskTableViewCell {
            
           
            var task = tasksArray[indexPath.row]
            self.tasksArray[indexPath.row] = task

            cell.refresh(task)
            
            cell.buttonTap = {
                task.isComleted = !task.isComleted
                switch task.isComleted {
                case true:
                    cell.checkButton.setImage(UIImage(named: "check"), for: .normal)
                   
                case false:
                    cell.checkButton.setImage(UIImage(named: "circle"), for: .normal)
                    
                }
            }
            
            return cell
        }
        
        return UITableViewCell()
        
    }
   

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.