我在同一个Collectionviewcell上有两个tableview . 当我运行应用程序时,它崩溃在tableview的cellforrowatindexpath中,说:“致命错误:在展开Optional值时意外发现nil”
请在下面找到我的一些代码 -
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as!
MyCustomCollectionViewCell
if(tableView == cell.graphTableView){
let cell:CustomGraphTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "CustomGraphTableViewCell") as!
CustomGraphTableViewCell
return cell
}
else
{
let cell:MyCustomTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as!
MyCustomTableViewCell
cell.nameLabel.text = namesArray[indexPath.row]
return cell
}
}
任何人都可以建议解决方案 . 它为什么会发生,同样的解决方案是什么?
2 回答
在tableview
UICollectionViewCell
不起作用,UICollectionViewCell
是从UICollectionReusableView
继承的不同类,UITableViewCell
是与UICollectionViewCell
不同的类,它继承自UIView
,NSCoding
,UIGestureRecognizerDelegate
,所以在这里,你必须只使用uitableview单元格作为你的tableview . 否则会崩溃 .当你的tableview委托方法返回一些值而不是null或0时,tableview的datasource方法将执行,这意味着当你的tableview的委托方法有一些值时
上面的方法现在将开始执行,当它找到时
以上声明,它将崩溃 .
这是
只将此语句放在cellforRow数据源方法下,它才会相应地工作 .
谢谢
请检查此行
cell.nameLabel
总是接受字符串值,首先转换为字符串值,如下所示: