Swift - 保存在TableView中选中的复选标记 [英] Swift - Save checkmarks selected in TableView

查看:107
本文介绍了Swift - 保存在TableView中选中的复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Swift相当新,我在TableView多选中遇到了问题。我有多个选项,我可以使用复选标记来检查,类似于待办事项列表。

I am fairly new to Swift and I am having an issue on TableView multi-selection. I have multiple selections that I can check with a checkmark, similar to to-do list.

当我检查项目时,我希望能够返回到ListView并保存我的选择。
我假设保存状态的代码会在这里的某个地方?这是写入单个单元格项目复选标记的类:

When I check the items I want to be able to come back to the ListView and have my selections saved. I'm assuming that the code to keep this in saved state would go somewhere here? This is the class where the individual cell item checkmark is written :

    class ListItem: NSObject {
    let itemName: String
    var completed: Bool

    init(itemName: String, completed: Bool = false)
    {
        self.itemName = itemName
        self.completed = completed
    }
}   

有人可以告诉我该怎么做?

Can Someone please show me how to go about this?

这是cellForRowAtIndexPath

This is the cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let tempCell = tableView.dequeueReusableCellWithIdentifier("ListPrototypeCell") as UITableViewCell
    let listItem = listItems[indexPath.row]

    // Downcast from UILabel? to UILabel
    let cell = tempCell.textLabel as UILabel!
    cell.text = listItem.itemName

    if (listItem.completed)
    {
        tempCell.accessoryType = UITableViewCellAccessoryType.Checkmark;
    }
    else
    {
        tempCell.accessoryType = UITableViewCellAccessoryType.None;
    }

    return tempCell
}    

和mySelectRowAtIndexPath

and my didSelectRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)

    let tappedItem = listItems[indexPath.row] as ListItem
    tappedItem.completed = !tappedItem.completed

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}    


推荐答案

两件事:

cellForRowAtIndexPath:中的代码是完美的。它从 listItems [indexPath.row] 获取信息来填充单元格的文本和复选标记。但是在你的 didSelectRowAtIndexPath:中,你永远不会改变 listItems ,这样 cellForRowAtIndexPath:具有更新的数据,在重新加载时从中提取新行。因此,不要使用 deselectRowAtIndexPath:,只需更新 listItems 数组,然后 reloadRowsAtIndexPaths 。 (老实说,我很困惑为什么它一直在为你工作,因为你在未更新的数据上调用reload。)这是我推荐的:

Your code in cellForRowAtIndexPath: is perfect. It takes the info from listItems[indexPath.row] to populate your cell's text and checkmark. But in your didSelectRowAtIndexPath:, you never change listItems such that cellForRowAtIndexPath: has the updated data from which to pull the new row upon reload. So instead of using deselectRowAtIndexPath:, simply update the listItems array then reloadRowsAtIndexPaths. (Honestly, I'm confused as to why it was working for you up to this point since you were calling reload on un-updated data.) Here's what I recommend:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let tappedItem = listItems[indexPath.row] as ListItem
    tappedItem.completed = !tappedItem.completed

    // Store the updated tappedItem back at the listItems index
    listItems[indexPath.row] = tappedItem

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

其次,只要由于该数组存储在未被解除分配的视图控制器中,因此您不会丢失该数据,并且您将能够重新填充该表。但如果它被存储在正被解除分配的视图控制器(或表视图控制器)中,您可能希望将其存储在程序的其他位置。

Secondly, as long as that array is being stored in a view controller that's not being deallocated, you won't lose that data and you'll be able to repopulate the table. But if it's being stored in a view controller (or table view controller) that's being deallocated, you may want to store it elsewhere in your program.

这篇关于Swift - 保存在TableView中选中的复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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