ios swift:Tableview“un"-复选标记单元格 [英] ios swift: Tableview "un"-checkmark cell

查看:31
本文介绍了ios swift:Tableview“un"-复选标记单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但我无法捕捉从 cellForRowAtIndexPathdidSelectRowAtIndexPath

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆