[英]fatal error: unexpectedly found nil while unwrapping an Optional value swift When Selecting tableView Cells
My tableView displays list of cards 我的tableView显示卡列表
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = cardsTable.dequeueReusableCellWithIdentifier("SelectableCardCell", forIndexPath: indexPath) as! CardSelectionTableViewCell
let cardCell: Card!
if self.delegate is CardsApprovalsViewController {
cardCell = approvalCards[indexPath.row] // approvalCard array of type card
} else {
cardCell = fetchedResultsController.objectAtIndexPath(indexPath) as! Card
}
cell.policyHolderName.text = cardCell.policy_holder_name
cell.alphaMark.text = cardCell.policy_holder_name.substringToIndex(advance(cardCell.policy_holder_name.startIndex, 1)).uppercaseString
var image: NSData = cardCell.photo as NSData
cell.picture.highlightedImage = UIImage(data: image)
cell.cardNumber.text = cardCell.member_id
cell.policyNumberLabel.text = cardCell.policy_no
cell.insuranceNameLabel.text = cardCell.insurance.company_name
return cell
}
Afetr selecting one cell, i want cell.alphaMark label to be hidden in order to display cell.picture.highlightedImage 选择一个单元格后,我想隐藏cell.alphaMark标签以显示cell.picture.highlightedImage
and if the user select another cell, cell.alphaMark for the previous cell should appear again 如果用户选择另一个单元格,则上一个单元格的cell.alphaMark应该再次出现
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell: CardSelectionTableViewCell = cardsTable.cellForRowAtIndexPath(indexPath) as! CardSelectionTableViewCell
selectedCell.alphaMark.hidden = true
let newSwiftColor = UIColor(red: 224, green: 227, blue: 224)
selectedCell.contentView.backgroundColor = newSwiftColor
if self.delegate is CardsApprovalsViewController {
self.card = approvalCards[indexPath.row]
} else {
self.card = fetchedResultsController.objectAtIndexPath(indexPath) as! Card
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell = cardsTable.cellForRowAtIndexPath(indexPath) as! CardSelectionTableViewCell // Some times crashes here
selectedCell.alphaMark.hidden = false
}
When i select cell and the select another cell ...etc (Selecting cells repeatedly) alphaMark and picture.highlightedImage works fine, but then it stops and gives me "unexpectedly found nil while unwrapping an Optional value" error 当我选择一个单元格并选择另一个单元格...等(反复选择单元格)时,alphaMark和picture.highlightedImage可以正常工作,但随后停止并给我“意外发现零,同时展开一个可选值”错误
I checked the outlets of the cell and they are connected to the storyboard 我检查了单元的插座,它们已连接到情节提要
Can someone help me with this error ? 有人可以帮我解决这个错误吗? Thanks 谢谢
The easiest thing to do to prevent the crashing, is to change from forced unwrapping of the optional and use an if let
instead: 要防止崩溃,最简单的方法是更改强制展开可选对象的方式,并使用if let
代替:
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let selectedCell = cardsTable.cellForRowAtIndexPath(indexPath) as? CardSelectionTableViewCell {
selectedCell.alphaMark.hidden = false
}
}
This will take into account that cardsTable.cellForRowAtIndexPath(indexPath)
may return nil, so trying to unwrap it into CardSelectionTableViewCell
won't fail (because you can't unwrap nil into a CardSelectionTableViewCell
) 这将考虑到cardsTable.cellForRowAtIndexPath(indexPath)
可能返回nil,因此尝试将其解包到CardSelectionTableViewCell
不会失败(因为您不能将nil解包到CardSelectionTableViewCell
)
That will prevent the crashing, but it won't entirely accomplish what you are trying to do. 这样可以防止崩溃,但是并不能完全完成您要尝试执行的操作。 You'll also need to update cellForRowAtIndexPath
to check if the current cell is selected
and update alphaMark.hidden
appropriately. 您还需要更新cellForRowAtIndexPath
以检查是否selected
了当前单元格,并适当地更新alphaMark.hidden
。
So one quick crack at how to do that would be to add this to the end of cellForRowAtIndexPath
so that each time the cell is brought on screen, it's alphaMark
is conditionally hidden based on the cell being selected: 因此,快速执行此操作的一个方法是将其添加到cellForRowAtIndexPath
的末尾,以便每次将单元格显示在屏幕上时,都会根据所选单元格有条件地隐藏其alphaMark
:
cell.alphaMark.hidden = cell.selected
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.