ios swift:Tableview“un”-checkmark cell

时间:2015-03-10 09:38:16

标签: ios uitableview swift

cellForRowAtIndexPath方法中我这样做,当我知道,有数据我可以勾选:

if (self.selectedLessonObject != nil){
    if(lessons["name"] as String == selectedLessonObject["name"] as String){
        cell.accessoryType = .Checkmark
    }
}

didSelectRowAtIndexPath内我做:

// update the checkmark for the current row
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark

我还能弄清楚如何在选择所选新单元格之前取消选择旧单元格?

如何在cellForRowAtIndexPath中保存所选行并在didSelectRowAtIndexPath中访问它以取消选择?

谢谢!

编辑:

当我尝试这个时:

var lastSelectedRow: NSIndexPath? = nil
cellForRowAtIndexPath

中的

if (self.selectedLessonObject != nil){
            if(lessons.objectId == selectedLessonObject.objectId){
                cell.accessoryType = .Checkmark
                lastSelectedRow = tableView.indexPathForSelectedRow()

            }else{
                cell.accessoryType = .None
            }
        }
println("selected: \(lastSelectedRow)")

我的输出为零。

didSelectRowAtIndexPath我想到了这个:

// Other row is selected - need to deselect it
// catch the previous selected row
var oldcell = tableView.cellForRowAtIndexPath(lastSelectedRow!)
oldcell?.accessoryType = .None

但我无法捕捉从cellForRowAtIndexPathdidSelectRowAtIndexPath

的当前行

3 个答案:

答案 0 :(得分:0)

您可以保存所选单元格的indexPath或行。或者你可以试着从中获取:

tableview.indexPathForSelectedRow()

答案 1 :(得分:0)

如果您在didSelectRowAtIndexPath中选择新行,请检查新属性是否为nill,如果是,则通过调用

获取单元格
if let last = lastSelectedRow {
    let oldSelectedCell = tableView.cellForRowAtIndexPath(last)
    oldSelectedCell.accessoryType = .None
}
lastSelectedRow = indexPath
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell.accessoryType = .Checkmark

cellForRowAtIndexPath中检查新属性是否为nill并且是否等于indexPath,否则选中复选标记取消选中:

if let last = lastSelectedRow {
    if (self.selectedLessonObject != nil){
        if(lessons.objectId == selectedLessonObject.objectId && last == indexPath){
            cell.accessoryType = .Checkmark

        }else{
            cell.accessoryType = .None
        }
    } else{
        cell.accessoryType = .None
    }
} else{
    if (self.selectedLessonObject != nil){
        if(lessons.objectId == selectedLessonObject.objectId){
            cell.accessoryType = .Checkmark
            lastSelectedRow = indexPath

        }else{
            cell.accessoryType = .None
        }
    }
}

答案 2 :(得分:0)

lastSelectedRow = indexPath
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell!.accessoryType = .Checkmark

        if selected.count > 0{
            if selected.containsObject(lastSelectedRow!.row){
                if cell!.accessoryType == .Checkmark{
                    cell!.accessoryType = .None
                    selected.removeObject(lastSelectedRow!.row)
                }else{
                    cell!.accessoryType = .Checkmark
                }
            }else{
                selected.addObject(lastSelectedRow!.row)
            }
        }else{
            selected.addObject(lastSelectedRow!.row)
        }