单击按钮时应用程序崩溃 --- 在 SWIFT 中
Posted
技术标签:
【中文标题】单击按钮时应用程序崩溃 --- 在 SWIFT 中【英文标题】:app crashes when a button is clicked --- in SWIFT 【发布时间】:2015-02-12 09:21:48 【问题描述】:早安
我有一个表格视图,启用了最多 5 个单元格的多重选择 .. 上面有 5 个按钮和图像的列表 .. 当用户点击一个单元格时,一张照片将出现在第一个空的 imageView
在表格上方,当用户单击该照片时,它应该会消失,并且在我的情况下,单元格颜色会恢复为透明或白色。
我的问题是,当我单击由于滚动而未显示单元格的照片时,应用程序崩溃。你能帮我吗..我如何检查索引路径是否显示,因为问题发生时它不是
这是我的代码:
@IBAction func btnClicked (sender : UIButton)
let tester = sender
if (tester == btn1 && img1.image != nil)
img1.image = nil
let cell1 = myTable.cellForRowAtIndexPath(img1IP)! as CustomCell
selectedIndexPaths.removeObject(img1IP)
configure(cell1, forRowAtIndexPath: img1IP)
counter--
else if (tester == btn2 && img2.image != nil)
img2.image = nil
let cell2 = myTable.cellForRowAtIndexPath(img2IP)! as CustomCell
selectedIndexPaths.removeObject(img2IP)
configure(cell2, forRowAtIndexPath: img2IP)
counter--
else if (tester == btn3 && img3.image != nil)
img3.image = nil
let cell3 = myTable.cellForRowAtIndexPath(img3IP)! as CustomCell
selectedIndexPaths.removeObject(img3IP)
configure(cell3, forRowAtIndexPath: img3IP)
counter--
else if (tester == btn4 && img4.image != nil)
img4.image = nil
let cell4 = myTable.cellForRowAtIndexPath(img4IP)! as CustomCell
selectedIndexPaths.removeObject(img4IP)
configure(cell4, forRowAtIndexPath: img4IP)
counter--
else if (tester == btn5 && img5.image != nil)
img5.image = nil
let cell5 = myTable.cellForRowAtIndexPath(img5IP)! as CustomCell
selectedIndexPaths.removeObject(img5IP)
configure(cell5, forRowAtIndexPath: img5IP)
counter--
else
println("its already empty")
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("intrest", forIndexPath: indexPath) as CustomCell
cell.selectionStyle = .None
cell.backgroundColor = UIColor.clearColor()
configure(cell, forRowAtIndexPath: indexPath)
return cell
func configure(cell: CustomCell, forRowAtIndexPath indexPath: NSIndexPath)
cell.lblInCell.text = InterstArr[indexPath.row]
if selectedIndexPaths.containsObject(indexPath)
// selected
cell.imgInCell.backgroundColor = UIColor.redColor()
else if (selectedIndexPaths.containsObject(indexPath) == false )
// not selected
cell.imgInCell.backgroundColor = UIColor.whiteColor()
cell.lblInCell.backgroundColor = UIColor.whiteColor()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
if selectedIndexPaths.containsObject(indexPath)
// deselect
println("remove object")
selectedIndexPaths.removeObject(indexPath)
////////////
if (img1str == InterstArr[indexPath.row])
img1.image = nil
println("remove 1 object")
else if ( img2str == InterstArr[indexPath.row])
img2.image = nil
println("remove 2 object")
else if (img3str == InterstArr[indexPath.row])
img3.image = nil
println("remove 3 object")
else if (img4str == InterstArr[indexPath.row])
img4.image = nil
println("remove 4 object")
else if ( img5str == InterstArr[indexPath.row])
img5.image = nil
println("remove 5 object")
counter--
// println(counter)
else if !(selectedIndexPaths.containsObject(indexPath))
// select
if (counter <= 4)
if (img1.image == nil)
img1str = InterstArr[indexPath.row]
img1.image = UIImage(named: InterstArr[indexPath.row])
img1IP = indexPath
println("add 1 object")
else if (img2.image == nil)
img2str = InterstArr[indexPath.row]
img2IP = indexPath
img2.image = UIImage(named: InterstArr[indexPath.row])
println("add 2 object")
else if (img3.image == nil)
img3str = InterstArr[indexPath.row]
img3IP = indexPath
img3.image = UIImage(named: InterstArr[indexPath.row])
println("add 3 object")
else if (img4.image == nil)
img4str = InterstArr[indexPath.row]
img4IP = indexPath
img4.image = UIImage(named: InterstArr[indexPath.row])
println("add 4 object")
else if (img5.image == nil)
img5str = InterstArr[indexPath.row]
img5IP = indexPath
img5.image = UIImage(named: InterstArr[indexPath.row])
println("add 5 object")
selectedIndexPaths.addObject(indexPath)
counter++
else if (counter > 4)
println("you selected 5 already")
let cell = tableView.cellForRowAtIndexPath(indexPath)! as CustomCell
configure(cell, forRowAtIndexPath: indexPath)
【问题讨论】:
你遇到了什么错误? @JoshHaringtonfatal error: unexpectedly found nil while unwrapping an Optional value
哪一行出现错误?
@JoshHarington 我刚刚添加了连接到按钮的@IBOutlet
函数.. 该行是let cell1 = myTable.cellForRowAtIndexPath(img1IP)! as CustomCell
,以防我在未显示单元格时单击第一个按钮
@JoshHarington 它是一个IndexPath
,它给出了一个错误,说CustomCell
is not convertable to UItableViewCell
【参考方案1】:
好吧..问题是如何检查是否显示indexPath
,所以这是我对那些有同样问题的人的回答,例如我只发布了功能的一部分,你可以重复@ 987654322@ 其他按钮.. 检查indexPath
是否可见的行是if ((myTable.cellForRowAtIndexPath(img1IP)) != nil)
:
@IBAction func btn1Clicked (sender : UIButton)
let tester = sender
if (tester == btn1 && img1.image != nil)
if ((myTable.cellForRowAtIndexPath(img1IP)) != nil)
println("1 exist")
let cell:CustomCell = myTable.cellForRowAtIndexPath(img1IP) as CustomCell!
cell.imgInCell.backgroundColor = UIColor.whiteColor()
img1.image = nil
selectedIndexPaths.removeObject(img1IP)
else
img1.image = nil
selectedIndexPaths.removeObject(img1IP)
【讨论】:
以上是关于单击按钮时应用程序崩溃 --- 在 SWIFT 中的主要内容,如果未能解决你的问题,请参考以下文章