从 Swift 中的表视图中删除单元格后设置正确的 uibutton 状态

set correct uibutton state after deleting cell from tableview in Swift

我是 Swift 的新手,我很难全神贯注于这个特定问题。所以请耐心一点,如果我不能马上传达这一点。 ;-) 谢谢!

基本上我在 UITable 中有一种待办事项列表。这些单元格有一个 UIButtons(带有自定义类)作为 "checkbox"。我将按钮状态存储在字典中,如下所示:

var buttonState = [listItem1 : true, listItem2 : false, ...]

词典也存储到NSUserDefaults。这一切都很好。我的问题是,当我使用 UITableViewCellEditingStyle.Delete 函数删除单元格并在使用 self.tableView.reloadData() 后重新加载 table 时,按钮状态出现错误。在上面的示例中,如果我删除了 listitem1,listItem2 将 displayed 为真。实际上存储的值仍然是假的。我想这一定与 indexPath 有关。

注意:我确实将 self.tableView.reloadData() 之前的词典更新为 buttonState[listItem1] = nil。这意味着我对字典中的每个 ListItem 都有正确的值。另外,如果我完全重新加载视图(即返回模拟器中的一个屏幕并从头开始重新加载列表),所有按钮状态都会正确显示。

删除 table 单元格后如何在不重新加载整个屏幕的情况下立即获得正确的按钮状态?

希望所有这些都能以某种方式说得通! ;-)

//七月

UITableView中的相关代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CellItem", forIndexPath: indexPath) as customCell

    //Identify Button with Tag
    cell.checkbox.tag = indexPath.row

    // Set Label
    switch indexPath.section{
    case 0:
        cell.textLabel?.text = "\(kleidung[indexPath.row])"
    case 1:
        cell.textLabel?.text = "\(hygiene[indexPath.row])"
    default:
        cell.textLabel?.text = nil
    }
    // Set Cell State
    cell.onLoadSet()
    return cell
}


// DELETE TABLE ROW
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if (editingStyle == UITableViewCellEditingStyle.Delete){

        if (indexPath.section == 0) {
            kleidung.removeAtIndex(indexPath.row)
            let fixedPackItems = ",".join(kleidung)
            packlisten![rowCount].setValue("\(fixedPackItems)", forKey: "kleidung")
        } else if (indexPath.section == 1) {
            hygiene.removeAtIndex(indexPath.row)
            let fixedPackItems = ",".join(hygiene)
            packlisten![rowCount].setValue("\(fixedPackItems)", forKey: "hygiene")
        }
        context.save(nil)

        if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
            var itemString = storedToDoItems as? Dictionary<String, Bool>
            state = itemString!
            println(itemString)
        }

        let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
        identifier = "\(currentCell.textLabel!.text!)\(rowCount)"
        state[identifier] = nil
        println(state)
        let checkedItems = state as Dictionary
        NSUserDefaults.standardUserDefaults().setObject(checkedItems, forKey: "packItems")
        NSUserDefaults.standardUserDefaults().synchronize()

        self.packlisteTable.reloadData()

    }
}

并且在 customCell 类中:

@IBAction func checkboxButton(sender: CheckBox) {

    // Change Button State
    if sender.isChecked == true {
        sender.isChecked = false
    }else{
        sender.isChecked = true
    }

    //Retrieve stored state
    if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
        var itemString = storedToDoItems as? Dictionary<String, Bool>
        if itemString != nil {
            state = itemString!
        }
    }

    //Add new State to Stored
    identifier = "\(textLabel!.text!)\(rowCount)"
        state[identifier] = sender.isChecked

    //Store state
    let checkedItems = state as Dictionary
    NSUserDefaults.standardUserDefaults().setObject(checkedItems, forKey: "packItems")
    NSUserDefaults.standardUserDefaults().synchronize()

}

func onLoadSet(){
    identifier = "\(textLabel!.text!)\(rowCount)"
    //Set state from stored State
    if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {

        if storedToDoItems != nil {
           var itemString = storedToDoItems as? Dictionary<String, Bool>
            if itemString![identifier] != nil {
                checkbox.isChecked = itemString![identifier] as Bool!
            }
        }
    }

}

好的,我找到了。没想到最后竟然这么简单!

我刚刚添加了一个 else 语句到:

func onLoadSet(){
identifier = "\(textLabel!.text!)\(rowCount)"
//Set state from stored State
if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {

    if storedToDoItems != nil {
       var itemString = storedToDoItems as? Dictionary<String, Bool>
        if itemString![identifier] != nil {
            checkbox.isChecked = itemString![identifier] as Bool!
        }
    }
}

工作代码:

func onLoadSet(){
    identifier = "\(textLabel!.text!)\(rowCount)"
    //Set state from stored State
    if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {

        if storedToDoItems != nil {
            println(storedToDoItems)
           var itemString = storedToDoItems as? Dictionary<String, Bool>
            if itemString![identifier] != nil {
                checkbox.isChecked = itemString![identifier] as Bool!
            } else {
                checkbox.isChecked = false
            }
        }
    }

}