ios - 表格 View 单元格无法进入 numberOfRowsInSection

标签 ios swift iphone xcode tableview

我有一些索引值每次都会发生变化。所以在我的 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 传递给行或我在这里做什么。任何帮助都有助于我理解这里。

最佳答案

您在 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()

然后看到魔法。

关于ios - 表格 View 单元格无法进入 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880158/

相关文章:

ios - 更新 DynamoDB (Swift) 中特定项目的属性

ios - swift 和 sprite 工具包在屏幕上拖动手指时画一条直线

iphone - 向上或向下滚动时UITableview崩溃以及如何为每个单元格设置水平 ScrollView

iphone - UITableView 单元格内存泄漏

iOS map View - 缩放以固定然后激活标注

ios - CDYelpFusionKit 初始化错误

swift - 为什么 TouchBegan 不能正常工作?

ios - 基于 ScrollView 在滚动上缩小导航栏

iphone - Interface Builder 文件中的未知类 myView

iphone - 在后台模式下播放音效