cellForRow (at:) возвращает ноль - PullRequest
       0

cellForRow (at:) возвращает ноль

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

Итак, у меня есть эта функция.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  
    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
    changeCellProperty(selectedIndexPath: indexPath)
    return cell;
}

func changeCellProperty(selectedIndexPath: IndexPath){
    print("indexpath = \(selectedIndexPath)") . // printing [0,0] and all values
    let cell = self.tableView.cellForRow(at: selectedIndexPath) as! customCell    
    // got nil while unwrapping error in above statement.

    cell.label.text = ""
    // and change other properties of cell.
}

Я не могу понять ошибку.Когда я получаю indexpath, то почему я не могу указать конкретную ячейку и соответственно изменить свойства.

1 Ответ

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

Вы не можете получить доступ к ячейке, которая еще не была добавлена ​​в tableView.Это то, что вы пытаетесь сделать здесь, в методе changeCellProperty.Таким образом, если ваша очередь работает, то все, что вам нужно сделать, это передать ячейку в очередь этому методу.

func changeCellProperty(cell: customCell){
     cell.label.text = ""
     // and change other properties of cell.
}

Ваш метод cellForRowAt будет выглядеть следующим образом.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
    changeCellProperty(cell: cell)
    return cell
}

Примечание: имена классов должны быть UpperCamelCase .Так что ваш customCell должен быть назван CustomCell.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...