Удаление строки из выбранной галочки в массиве в Swift - PullRequest
0 голосов
/ 01 мая 2018

Я хочу удалить данные из невыбранной строки с помощью галочки. Теперь я могу добавить строки в мой массив, которые проверены. Но я хочу удалить непроверенные данные из массива. Как я могу удалить непроверенные данные из моего массива?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    if(tableView == self.tableView_1) {

        if(tableView_1.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.none) {
            tableView_1.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark

            checkedItemsForGender.append(cinsiyet[indexPath.row])

        }
        else{
            tableView_1.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none

            checkedItemsForGender.remove(at: cinsiyet[indexPath.row]) // ??? ERROR 
        }

    }

Ответы [ 2 ]

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

Обновите else, как показано ниже, чтобы исправить error и возможность index out of bound exception

if let index = checkedItemForGender.index(of: cinsiyet[indexPath.row]),
      index < checkedItemsForGender.count {
     checkedItemsForGender.remove(at: index)
}
0 голосов
/ 01 мая 2018

Вы должны использовать это:

if let index = checkedItemForGender.index(of: cinsiyet[indexPath.row]) {
checkedItemsForGender.remove(at: index)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...