【问题标题】:Table view cell not able to get in numberOfRowsInSection表格视图单元格无法进入 numberOfRowsInSection
【发布时间】:2019-02-26 07:13:32
【问题描述】:

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

【问题讨论】:

  • 你不能直接返回List!.count 吗?
  • 不,因为我的列表在我的表格视图单元格中。我将其附加在索引路径方法的单元格中
  • 那么你从哪里得到这份清单?不能按照上面提到的方式来做,因为 numberOfRows 方法是在 cellForRowAt 方法之前调用的(我的意思是在单元格创建之前)
  • 查看我的更新帖
  • 我的第一条评论仍然有效,你试过了吗?那输出是什么?

标签: ios swift iphone xcode tableview


【解决方案1】:

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

看看魔法吧。

【讨论】:

  • 在我的vc中我最初声明lazy var List: [[String: Any]]? = FetchInfoUtil.sharedInstance.List
  • 但是如何在委托方法之外使用我的表格视图单元格标签?
  • 为什么要在委托方法外使用单元格标签?
  • 但在某些地方会崩溃
  • 我需要传递索引值。或者因为我在顶部有一个页面菜单控制器。单击任何菜单名称时,我必须在每个菜单项下的底部显示一个表格视图。现在我通过索引值单击菜单项..基于该 ia m 显示数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多