Ios 在SWIFT中单击按钮时应用程序崩溃
早上好 我有一个支持最多5个单元格的多像素选择的表视图。。上面有5个按钮和图像的列表。。当用户点击一个单元格时,一张照片将出现在表格上方的第一个空图像视图中,当用户点击该照片时,它将消失,在我的情况下,单元格颜色将恢复为清晰或白色 我的问题是,当我点击一张因滚动而未显示的照片时,应用程序崩溃。你能帮帮我吗。。我如何检查indexpath是否显示,因为问题是在未显示时发生的 这是我的密码:Ios 在SWIFT中单击按钮时应用程序崩溃,ios,uitableview,swift,cells,nsindexpath,Ios,Uitableview,Swift,Cells,Nsindexpath,早上好 我有一个支持最多5个单元格的多像素选择的表视图。。上面有5个按钮和图像的列表。。当用户点击一个单元格时,一张照片将出现在表格上方的第一个空图像视图中,当用户点击该照片时,它将消失,在我的情况下,单元格颜色将恢复为清晰或白色 我的问题是,当我点击一张因滚动而未显示的照片时,应用程序崩溃。你能帮帮我吗。。我如何检查indexpath是否显示,因为问题是在未显示时发生的 这是我的密码: @IBAction func btnClicked (sender : UIButton) { l
@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,因此这里是我对那些有相同问题的人的回答,例如,我只发布了函数的一部分,如果tester==btn1&&img1.image!=其他按钮为零。。如果myTable.cellforrowatinedexpathimg1ip!=零:
}您遇到了什么错误?@JoshHarington致命错误:在展开可选值时意外地发现了零,哪一行给出了错误?@JoshHarington我刚刚添加了连接到按钮的@IBOutlet函数。。行为cell1=myTable.cellforrowatinexpathimg1ip!作为CustomCell,以防我在第一个按钮上单击时其单元格未被选中shown@JoshHarington它是一个IndexPath,并给出一个错误,表示CustomCell不可转换为UItableViewCell
@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)
}