在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但是我无法从cellForRowAtIndexPath捕获当前行到didSelectRowAtIndexPath
发布于 2015-03-10 09:51:50
如果在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
}
}
}发布于 2015-03-10 09:46:17
可以保存选定单元格的indexPath或行。或者你可以试着从:
tableview.indexPathForSelectedRow()https://stackoverflow.com/questions/28960263
复制相似问题