[英]It crashes in cellforrowatindexpath of tableview , says : “fatal error: unexpectedly found nil while unwrapping an Optional value”
I have two tableview on the same Collectionviewcell. 我在同一Collectionviewcell上有两个tableview。 When I run the application, it crashes in cellforrowatindexpath of tableview , says : "fatal error: unexpectedly found nil while unwrapping an Optional value"
当我运行应用程序时,它在tableview的cellforrowatindexpath中崩溃,并说:“致命错误:在展开可选值时意外发现nil”
Please find below some of my code - 请在下面找到我的一些代码-
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
}
}
Can anyone please suggest the solution. 任何人都可以建议解决方案。 Why is it happening and what can be the solution for the same?
为什么会发生这种情况,对于相同的解决方案有什么解决方案?
In tableview UICollectionViewCell
won't work, UICollectionViewCell
is different class which is inherited from UICollectionReusableView
and UITableViewCell
is a different class from UICollectionViewCell
and it is inherited from UIView
, NSCoding
, UIGestureRecognizerDelegate
, so here, you have to use only uitableview cell for your tableview. 在实现代码如下
UICollectionViewCell
将无法正常工作, UICollectionViewCell
是从继承不同的类UICollectionReusableView
和UITableViewCell
是从不同的类UICollectionViewCell
并从继承UIView
, NSCoding
, UIGestureRecognizerDelegate
,所以在这里,你必须只使用电池的UITableView为您实现代码如下。 Else it will crash. 否则它将崩溃。
When your tableview delegate method return some value not but null or 0, then datasource method of tableview will execute, that means when your tableview's delegate method have some value the 当您的tableview委托方法返回的值不是null或0时,则将执行tableview的datasource方法,这意味着当tableview的委托方法具有某些值时,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell
above method will now started executing and when it found 上面的方法现在将开始执行,并在发现时
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as!
MyCustomCollectionViewCell
above statement, it will get crashed. 上面的声明,它将崩溃。
This is 这是
let cell:MyCustomTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as!
MyCustomTableViewCell
cell.nameLabel.text = namesArray[indexPath.row]
return cell
only put this statement under the cellforRow datasource method, it will work accordingly. 仅将此语句放在cellforRow数据源方法下,它将相应地工作。
Thanks 谢谢
Please check this line 请检查此行
cell.nameLabel.text = namesArray[indexPath.row]
The cell.nameLabel
is always accept string value, first convert into string value like this: cell.nameLabel
始终接受字符串值,首先将其转换为如下所示的字符串值:
cell.nameLabel.text = namesArray[indexPath.row] as NSString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.