[英]app crashes when a button is clicked — in SWIFT
早上好
我有一个桌面视图,最多可以选择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)
}
好的,那么..问题是如何检查是否显示了indexPath
,所以这是我针对那些有相同问题的人的答案,例如,我仅发布了该函数的一部分, if (tester == btn1 && img1.image != nil)
其他按钮.. if ((myTable.cellForRowAtIndexPath(img1IP)) != nil)
,检查indexPath
是否可见的行就是这个:
@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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.