【发布时间】: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