简体   繁体   English

它在tableview的cellforrowatindexpath中崩溃,表示:“致命错误:在展开可选值时意外发现nil”

[英]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是从继承不同的类UICollectionReusableViewUITableViewCell是从不同的类UICollectionViewCell并从继承UIViewNSCodingUIGestureRecognizerDelegate ,所以在这里,你必须只使用电池的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.

相关问题 致命错误:在Tableview中展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Tableview 致命错误:解开可选值(cellForRowAtIndexPath)时意外发现nil(Xib自定义单元格Swift) - fatal error: unexpectedly found nil while unwrapping an Optional value (cellForRowAtIndexPath) (Xib Custom Cell Swift) 在iOS中,继续收到“在解包可选值时意外发现nil”的致命错误 - Keep getting a fatal error that says “unexpectedly found nil while unwrapping an Optional value” in iOS 致命错误:选择tableView单元格时,在展开可选值swift时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value swift When Selecting tableView Cells 快速搜索栏和表格视图-致命错误:在展开可选值时意外发现nil - swift searchbar and tableview - fatal error: unexpectedly found nil while unwrapping an optional value ARQuicklook-致命错误:展开一个可选值时意外发现nil - ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在AVFoundation中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in AVFoundation 致命错误:展开一个可选值NSURL时意外发现nil - Fatal Error: Unexpectedly found nil while unwrapping an Optional value NSURL 致命错误:在展开可选值时意外发现 nil:文件 - Fatal error: Unexpectedly found nil while unwrapping an Optional value: file 致命错误:解开可选值计算时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value Computation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM