1

我有一个 30ish 的 lang 表,分为 4 个部分..

我的问题是,当我想更改其中一个单元格中的值时,该值不会更新/更改,直到我将该单元格滚动出视图并返回..

我使用 UIAlert 输入新值,然后将其保存到数据库中。并且该值已保存好,然后我再次从数据库中取回正确的值。

但是当我重新加载tableView它并没有显示新值..

Bouns:我有两个 tableView 一个名为:templateTableView和一个名为:fieldTableViewfieldTableView是我正在尝试更改值的那个。更改tableView.reload()self!.fieldTableView.reload()也不起作用。

从 didSelectRowAtIndexPath:

}else if currTemplate!.hasPassFail{
            var alertController:UIAlertController?
            alertController = UIAlertController(title: "Enter new value",
                message: "Enter new value",
                preferredStyle: .Alert)

            alertController!.addTextFieldWithConfigurationHandler(
                {(textField: UITextField!) in
                    textField.placeholder = ""
                    textField.keyboardType = UIKeyboardType.NumbersAndPunctuation
            })

            let action = UIAlertAction(title: "Submit",
                style: UIAlertActionStyle.Default,
                handler: {[weak self]
                    (paramAction:UIAlertAction!) in
                    if let textFields = alertController?.textFields{
                        let theTextFields = textFields as! [UITextField]
                        let enteredText = theTextFields[0].text
                        //what to do after edit, here:
                        switch indexPath.section{
                        case 1:
                            if let value = enteredText.toInt(){
                                self!.currTemplate!.backgroundMin[indexPath.row] = value
                            }else{
                                self!.presentViewController(self!.wrongNumberAlert, animated: true, completion: nil)
                                return
                            }
                        case 2:
                            if let value = enteredText.toInt(){
                                self!.currTemplate!.legendMin[indexPath.row] = value
                            }else{
                                self!.presentViewController(self!.wrongNumberAlert, animated: true, completion: nil)
                                return
                            }
                        case 3:
                            if let value = enteredText.toDouble(){
                                self!.currTemplate!.ratioMin[indexPath.row] = value
                            }else{
                                self!.presentViewController(self!.wrongNumberAlert, animated: true, completion: nil)
                                return
                            }
                        default:
                            0-0
                        }
                        var date = NSDate()
                        self!.currTemplate!.timestamp = date.makeTimestampString()
                        self!.appDelegate.dbInterface.updateTemplate(self!.currTemplate!)
                        //self!.templateArr = self!.appDelegate.dbInterface.findTemplates()
                        self!.findAllNotDeleteOnSync()
                        println("Data was added to DB: \(self!.currTemplate!.ratioMin[indexPath.row])")
                        self!.templateTableView.reloadData()
                        self!.templateTableView.selectRowAtIndexPath(self!.currentTemplateIndex, animated: false, scrollPosition: .None)
                        dispatch_async(dispatch_get_main_queue(),{
                            tableView.reloadData()
                        })
                    }
                })
            let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil)
            alertController?.addAction(cancel)
            alertController?.addAction(action)
            self.presentViewController(alertController!, animated: true, completion: nil)
        }

编辑 应该工作的代码部分是这样的:

dispatch_async(dispatch_get_main_queue(),{
   tableView.reloadData()
})
4

4 回答 4

1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    // do DB stuff in background thread
    // ...

    dispatch_async(dispatch_get_main_queue(), {
        // Update UI in main thread when done
        // ...
    })
})
于 2015-08-24T11:59:50.493 回答
1

此线路导致故障

if indexPath.row == 0 && indexPath.section == 1

仔细查看您在这种情况下所写的内容,您仅在满足条件的情况下附加数据,但是在这种情况下,您的didselectrowatindexpath

case 3: 
if let value = enteredText.toDouble(){ 
self!.currTemplate!.ratioMin[indexPath.row] = value 
}else{ 
self!.presentViewController(self!.wrongNumberAlert, animated: true, completion: nil) 
return 
}

它失败了,这就是为什么当您在第 2 节和第 3 节中的编辑行将附加代码移动到其他地方或分段使用if indexPath.row == 0 && indexPath.section == 1此条件时,没有附加数据的原因

于 2015-08-24T12:31:00.087 回答
0

改用这个:

tableView.reloadData()
于 2015-08-24T11:44:53.617 回答
0

为我工作:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    dispatch_async(dispatch_get_main_queue(), {
        // Update UI in main thread when done
        tableView.reloadData()
    })
})
于 2016-03-09T03:08:23.610 回答