ios – 如何在滚动tableview后点击选择行时修复崩溃?

ios – 如何在滚动tableview后点击选择行时修复崩溃?,第1张

概述我有一个像这样的表格视图: 当用户点击行时,我想取消选中最后一行并检查所选行.所以我写了这样的代码: (例如我的lastselected = 0) func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var lastIndexPath:NSIndexPa 我有一个像这样的表格视图:

当用户点击一行时,我想取消选中最后一行并检查所选行.所以我写了这样的代码:
(例如我的lastselected = 0)

func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) {        var lastIndexPath:NSIndexPath = NSIndexPath(forRow: lastSelected,inSection: 0)        var lastCell = self.diceFacetable.cellForRowAtIndexPath(lastIndexPath) as! tableVIEwCell        var cell = self.diceFacetable.cellForRowAtIndexPath(indexPath) as! tableVIEwCell        lastCell.checkimg.image = UIImage(named: "uncheck")        cell.checkimg.image = UIImage(named: "check")        lastSelected = indexPath.row}

当我点击一行而不滚动时,每件事情都很好.我意识到当我运行代码并立即滚动表并选择一行时.我的程序会因错误而崩溃
“致命错误:在展开Optional值时意外发现nil”

错误显示在这一行:

我不知道这里有什么不对?

解决方法 因为当您尝试选择不在屏幕中的单元格时使用可重用单元格时,应用程序将崩溃,因为内存中不再存在单元格,请尝试以下 *** 作:

if let lastCell = self.diceFacetable.cellForRowAtIndexPath(lastIndexPath) as! tableVIEwCell{    lastCell.checkimg.image = UIImage(named: "uncheck")}//update the data set from the table vIEw with the change in the icon//for the old and the new cell

如果单元格当前在屏幕中,此代码将更新复选框.如果当您重新使用单元格时(dequeuereusablecellwithIDentifIEr)当前不在屏幕上,则应在显示之前正确设置它.为此,您需要更新表视图的数据集以包含更改.

总结

以上是内存溢出为你收集整理的ios – 如何在滚动tableview后点击选择行时修复崩溃?全部内容,希望文章能够帮你解决ios – 如何在滚动tableview后点击选择行时修复崩溃?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1067471.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存