Флажок установлен и не отмечен, не сохраняется в моем представлении таблицы - PullRequest
0 голосов
/ 11 октября 2018

Моя задача Я пытаюсь показать tableView с помощью кнопки check box внутри popup viewcontroller.

Всякий раз, когда пользователь нажимает button, это меняет чек и uncheck, но мне нужно сделать, когда я нажимаю «Готово» button, я должен сохранить пользователя checked, а если нажать cancel, он должен uncheck (который проверяется пользователем перед нажатием отмены).Мне нужно понять и исправить эту задачу.

Check and Uncheck popup

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()
}

1 Ответ

0 голосов
/ 11 октября 2018

Вы можете рассмотреть возможность использования базовой ячейки taleview с установкой acessoryType.Установка флажка и надписей слева, кажется, дает плохой UX

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

    let category = defectSet [indexPath.section]
    let item = category.items[indexPath.row]

    cell.textLabel?.text = item.name
    cell.selectionStyle = .none

    let isFound = User.shared.selectedDefect.filter{ $0.id == item.id }.count > 0

    if (isFound)
    {
        cell.accessoryType = .checkmark
    }
    else
    {
        cell.accessoryType = .none
    }
    return cell
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...