ios swift:Tableview“un"-复选标记单元格 [英] ios swift: Tableview "un"-checkmark cell
问题描述
在 cellForRowAtIndexPath
方法中,当我知道有数据时,我可以检查:
within the cellForRowAtIndexPath
method I do this, when I know, that there is data I can checkmark:
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
我还不知道如何在选择新单元格之前先取消选择旧单元格?
What I can't figure out yet is how to deselect FIRST the old cell before I select the chosen new cell?
如何将选定的行保存在 cellForRowAtIndexPath
中并在 didSelectRowAtIndexPath
中访问它以取消选择它?
How can I save the selected row in cellForRowAtIndexPath
and access it in didSelectRowAtIndexPath
to unselect it?
谢谢!!
当我尝试这个时:
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
中我想到了这个:
In didSelectRowAtIndexPath
the I thought about this:
// Other row is selected - need to deselect it
// catch the previous selected row
var oldcell = tableView.cellForRowAtIndexPath(lastSelectedRow!)
oldcell?.accessoryType = .None
但我无法捕捉从 cellForRowAtIndexPath
到 didSelectRowAtIndexPath
But I am not able to catch the current row from cellForRowAtIndexPath
to didSelectRowAtIndexPath
推荐答案
如果在 didSelectRowAtIndexPath
中选择新行,请检查新属性是否为空,如果是,则通过调用
If you select new row in didSelectRowAtIndexPath
check if the new property is not nill and if so get cell by calling
if let last = lastSelectedRow {
let oldSelectedCell = tableView.cellForRowAtIndexPath(last)
oldSelectedCell.accessoryType = .None
}
lastSelectedRow = indexPath
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell.accessoryType = .Checkmark
在 cellForRowAtIndexPath
中检查新属性是否为空且是否等于 indexPath 并勾选否则取消勾选:
In cellForRowAtIndexPath
check if the new property is not nill and is equal indexPath and checkmark otherwise uncheckmark:
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
}
}
}
这篇关于ios swift:Tableview“un"-复选标记单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!