致命错误:在didDeselectRowAt中解包可选值时发现意外的nil

问题描述:

对于IOS开发我绝对是新手,并且陷入了这个问题。我正在尝试一个非常简单的事情,我希望当我在TableView中选择一个单元格时,它的背景颜色会变成灰色,当我选择另一个单元格时,以前的灰色单元格的背景颜色应该恢复为默认颜色。要做到这一点,我使用下面的代码: -致命错误:在didDeselectRowAt中解包可选值时发现意外的nil

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{  
    let currentCell = tableView.cellForRow(at: indexPath) as! RoundTableViewCell 
    currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#d3d3d3") 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
{ 
    let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell // Error 
    currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff")  
} 

上面的代码工作得很好,但是当我尝试挖掘新的小区,而先前所选的小区不在表视图,因为它看到它崩溃滚动。只是无法理解这里发生了什么。任何帮助,将不胜感激。谢谢。

编辑

cellForRowAt

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell: RoundTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "cell")! as? RoundTableViewCell)! 
    cell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffff  
    return cell 
} 
+0

您能显示'cellForRow'方法的实现吗?顺便说一句,你不需要做'as! RoundTableViewCell'在你的情况下,只需删除,一切都应该没问题。 –

+0

好吧,我会尽力让你知道。 –

+1

查阅文档:https://developer.apple.com/reference/uikit/uitableview/1614983-cellforrow:代表表格单元格的对象,或者**如果单元格不可见,则为nil ** ... –

您需要检查,如果小区中的可用与否

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
{ 
    if let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff")  
    } 
} 
+0

哇!感谢它现在的作品。 –

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    .... 
    .... 
    if cell.isSelected == true { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#d3d3d3")  
    } 
    else { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff") 
    } 
    .... 
    .... 
} 

如果你想多单元选择。

table.allowsMultipleSelection = true 

应用程序崩溃,因为当单元格从屏幕消失时,它从内存中释放,所以你得到零值。那么试试这个

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{  
    if let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#d3d3d3")  
    } 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
{ 
    if let currentCell = tableView.cellForRow(at: indexPath) as? RoundTableViewCell { 
     currentCell.contentView.backgroundColor = Util.hexStringToUIColor(hex: "#ffffff")  
    } 
} 

使用下面的代码来获得预期的结果。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell 


     cell.selectedBackgroundView = UIImageView() 
     cell.backgroundView=UIImageView() 

     let selectedBackground : UIImageView = cell.selectedBackgroundView as! UIImageView 
     selectedBackground.image = UIImage.init(named:"selected.png"); 

     let backGround : UIImageView = cell.backgroundView as! UIImageView 
     backGround.image = UIImage.init(named:"defaultimage.png"); 

     return cell 


    }