ios swift:Tableview“;联合国-复选标记单元格

ios swift:Tableview“;联合国-复选标记单元格,ios,uitableview,swift,Ios,Uitableview,Swift,在cellforrowatinexpath方法中,当我知道有数据可以勾选时,我会这样做: if (self.selectedLessonObject != nil){ if(lessons["name"] as String == selectedLessonObject["name"] as String){ cell.accessoryType = .Checkmark } } if let last = lastSelectedRow { if (

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

if (self.selectedLessonObject != nil){
    if(lessons["name"] as String == selectedLessonObject["name"] as String){
        cell.accessoryType = .Checkmark
    }
}
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
        }
    }
}
didselectrowatinexpath
中,我执行以下操作:

// update the checkmark for the current row
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
我还不知道如何在选择新单元格之前先取消选择旧单元格

如何将所选行保存在
单元格中,然后在
didselectrowatinexpath
中访问以取消选中它

谢谢

编辑:

当我尝试这个:

var lastSelectedRow: NSIndexPath? = nil
cellforrowatinexpath

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

            }else{
                cell.accessoryType = .None
            }
        }
println("selected: \(lastSelectedRow)")
我得到一个零输出

didselectrowatinexpath
中,我想到了这个:

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

但是我无法从
cellForRowAtIndexPath
捕获当前行到
didSelectRowAtIndexPath
您可以保存选定单元格的indexath或行。或者,您可以尝试从以下位置获取:

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

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

如果在
didselectRowatinedexpath
中选择新行,请检查新属性是否为nill,如果为nill,则通过调用获取单元格

if let last = lastSelectedRow {
    let oldSelectedCell = tableView.cellForRowAtIndexPath(last)
    oldSelectedCell.accessoryType = .None
}
lastSelectedRow = indexPath
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell.accessoryType = .Checkmark
cellforrowatinexpath
中,检查新属性在XPath中是否为非nill且等于,否则取消选中标记:

if (self.selectedLessonObject != nil){
    if(lessons["name"] as String == selectedLessonObject["name"] as String){
        cell.accessoryType = .Checkmark
    }
}
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
        }
    }
}

@用户1555112您尝试了什么?你能发布你的代码吗?您必须将代码添加到cellForRow和didSelectRow方法中。@user1555112请查看我修改后的答案。在哪里以及作为什么类型需要设置
lastSelectedRow
?因为现在我还没有到达if构造,因为var在正确的开头被设置为nil。@user1555112我再次编辑了我的过去。我看不到所有的代码,很难为您提供解决方案。如果您仅对mark selectedLessonObject.objectId使用复选标记,则可以在didSelectRow中为该对象添加值,并在这种情况下重新加载表视图,您甚至不需要lastSelectedRow。我现在做对了,非常感谢!这对我帮助很大!!谢谢所选变量:NSMutableArray!