Ios 表格视图单元格无法进入numberOfRowsInSection
我有一些索引值每次都会改变。所以在我的vc中:Ios 表格视图单元格无法进入numberOfRowsInSection,ios,swift,iphone,xcode,tableview,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(withIdenti
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()
看看神奇之处。你就不能返回
List!。计数
?否,我的列表在我的表格视图单元格中。我在单元格中为索引路径方法处的行添加了该列表,那么从何处获取该列表呢?你不能用上面提到的方法来做,因为numberOfRows方法是在cellForRowAt方法之前调用的(我的意思是在创建单元格之前)检查我的更新帖子。我的第一条评论是有效的,你试过了吗?它的输出是什么?在我的vc中,我初始声明了lazy var List:[[String:Any]]?=FetchInfoUtil.sharedInstance.List
但是我如何在委托方法之外使用我的表视图单元格标签?为什么要在委托方法之外使用单元格标签?但是在某些地方出现崩溃我需要传递索引值..或者bec我在顶部有一个页面菜单控制器。单击任何菜单名,我必须在每个菜单项下的底部显示一个表视图。现在我正在传递索引值,单击菜单项..基于此,我显示数据