我有一个 30ish 的 lang 表,分为 4 个部分..
我的问题是,当我想更改其中一个单元格中的值时,该值不会更新/更改,直到我将该单元格滚动出视图并返回..
我使用 UIAlert 输入新值,然后将其保存到数据库中。并且该值已保存好,然后我再次从数据库中取回正确的值。
但是当我重新加载tableView
它并没有显示新值..
Bouns:我有两个 tableView 一个名为:templateTableView
和一个名为:fieldTableView
。fieldTableView
是我正在尝试更改值的那个。更改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()
})