Ios 它在tableview的cellforrowatindexpath中崩溃,显示:“致命错误:在展开可选值时意外发现nil”
我在同一个Collectionviewcell上有两个tableview。当我运行应用程序时,它在tableview的cellforrowatindexpath中崩溃,显示:致命错误:在展开可选值时意外发现nil 请在下面找到我的一些代码-Ios 它在tableview的cellforrowatindexpath中崩溃,显示:“致命错误:在展开可选值时意外发现nil”,ios,iphone,swift,uitableview,swift3,Ios,Iphone,Swift,Uitableview,Swift3,我在同一个Collectionviewcell上有两个tableview。当我运行应用程序时,它在tableview的cellforrowatindexpath中崩溃,显示:致命错误:在展开可选值时意外发现nil 请在下面找到我的一些代码- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = collectionView.d
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
}
}
谁能提出解决办法。为什么会发生这种情况以及解决方案是什么?在tableview中,UICollectionViewCell不起作用,UICollectionViewCell是从UICollectionReusableView继承的不同类,UITableViewCell是从UICollectionViewCell继承的不同类,它是从UIView、NSCoding、UIgestureRecognitizerDelegate、,因此,在这里,您必须仅为tableview使用uitableview单元格。否则它会崩溃
当您的tableview委托方法返回某个值而不是null或0时,tableview的datasource方法将执行,这意味着当您的tableview委托方法具有某个值时
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell
上述方法现在将开始执行,并在找到它时执行
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as!
MyCustomCollectionViewCell
在上面的声明中,它将崩溃
这是
let cell:MyCustomTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as!
MyCustomTableViewCell
cell.nameLabel.text = namesArray[indexPath.row]
return cell
仅将此语句置于cellforRow数据源方法下,它将相应地工作
谢谢请检查这行
cell.nameLabel.text = namesArray[indexPath.row]
cell.nameLabel始终接受字符串值,首先将其转换为字符串值,如下所示:
cell.nameLabel.text = namesArray[indexPath.row] as NSString
您无法在TableView cellForRowAt`方法中退出CollectionViewCell``的队列。您可以在此处强制展开为!检查所有单元格,因为现在nil@NiravD如果我不在那里声明collectionview单元格,你知道如何访问tableview吗?@Vadim Kozak Ok。如果为零,我可以做些什么来更正它?为什么使用collectionview单元格而不是tableview单元格?collectionview单元格在tableview中不能作为单元格使用。谢谢阿披谢克!是的,我从cellforrowatindexpath中删除了collectionviewcell对象,并将标记分配给tableview进行检查,现在它工作正常:很好,:很高兴编码,如果我的建议对您有效,请接受并投票表决我的答案。。谢谢: