Ячейка табличного представления не может получить в numberOfRowsInSection - PullRequest
0 голосов
/ 26 февраля 2019

У меня есть некоторые значения индекса, которые будут меняться каждый раз.Так что в моем VC:

lazy var List: [[String: Any]]? = FetchInfoUtil.sharedInstance.List

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell", for: indexPath) as! MainTableViewCell
       print(indexPath.row)

        switch CurrentIndexVal {
        case 1:
            cell.Info = List?[0];
        case 2:
            cell.Info = List?[1];
        case 3:
            cell.Info = List?[2];
        case 4:
            cell.Info = List?[3];
        case 5:
            cell.Info = List?[4];
        case 6:
            cell.Info = List?[5];
        default:
            break;
        }

         if let gList = cell.Info?["list"]  as? [[String: Any]] {
            SelectedSkill = gList
            let gInfo = gList[indexPath.item]
            cell.Name.text = gInfo["title"] as? String ?? "NA"
        }

        return cell
    }

и вот проблемы:

 func tableView(_ tableView: UITableView,     section: Int) -> Int {
       let indexPath = IndexPath.init(row: 0, section: 0)
       let cell = tableView.cellForRow(at: indexPath) as? MainTableViewCell
        guard let gList = cell?.Info?["list"]  as? [[String: Any]] else {
            return 0
       }
       return gList.count;
}

Здесь я получаю сбой каждый раз: let cell = tableView.cellForRow(at: indexPath) as? MainTableViewCell

Если мне нужно пройтимои currentIndexVal в строке или что я здесь делаю. Любая помощь будет полезна для меня здесь.

1 Ответ

0 голосов
/ 26 февраля 2019

Вы используете неправильный подход в вашем numberOfRows методе.Ваша клеточная нагрузка после этого метода numberOfRows.Текущая ячейка не имеет списка.Таким образом, в этом методе сначала получите listObject, а затем верните его количество.Попробуйте использовать следующий подход ..

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard let gList = List?[CurrentIndexVal]["list"]  as? [[String: Any]] else {
            return 0
       }
       return gList.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell", for: indexPath) as! MainTableViewCell
       print(indexPath.row)
        let dictionaryObject = List?[CurrentIndexVal]
         if let gList = dictionaryObject?["list"]  as? [[String: Any]] {
            SelectedSkill = gList
            let gInfo = gList[indexPath.item]
            cell.Name.text = gInfo["title"] as? String ?? "NA"
        }

        return cell
    }

И когда вы когда-нибудь нажмете кнопку меню и обновите CurrentIndexVal сразу после обновления значения в CurrentIndexVal, просто позвоните.

self.tableView.reloadData()

И увидеть магию.

...