Моя задача Я пытаюсь показать tableView
с помощью кнопки check box
внутри popup
viewcontroller.
Всякий раз, когда пользователь нажимает button
, это меняет чек и uncheck
, но мне нужно сделать, когда я нажимаю «Готово» button
, я должен сохранить пользователя checked
, а если нажать cancel
, он должен uncheck
(который проверяется пользователем перед нажатием отмены).Мне нужно понять и исправить эту задачу.
![Check and Uncheck popup](https://i.stack.imgur.com/QPQlk.png)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.myCellLabel.text = self.animals[indexPath.row]
if selectedRows.contains(indexPath)
{
cell.checkBox.setImage(UIImage(named:"check.png"), for: .normal)
}else{
cell.checkBox.setImage(UIImage(named:"uncheck.png"), for: .normal)
}
cell.checkBox.tag = indexPath.row
cell.checkBox.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell else {
return
}
if self.selectedRows.contains(indexPath) {
self.selectedRows.remove(at: self.selectedRows.index(of: indexPath)!)
cell.checkBox.setImage(UIImage(named:"uncheck.png"), for: .normal)
} else {
self.selectedRows.append(indexPath)
cell.checkBox.setImage(UIImage(named:"check.png"), for: .normal)
let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
let currentCell = tableView.cellForRow(at: indexPath!) as! MyCustomCell
}
}
@IBAction func cancelPopup(_ sender: Any) {
self.removeAnimate()
}
@IBAction func donePopUp(_ sender: AnyObject) {
self.removeAnimate()
}