Bjqn
Bjqn

Reputation: 146

Swift: UITableView does not update data properly using reload()

I have a 30ish lang table divided 4 sections..

My problem is that, when i want to change the an value in one of the cells, the value doesn't update/change until I scroll that cell out of view and back in..

I use a UIAlert, to enter the new value, a save it to a DB.. And the value is saved alright, and i get the right value back again from the DB..

But when i reload the tableView it doesn't show the new value..

Bouns: I have two tableView one named: templateTableView and one named: fieldTableView. fieldTableView is the one i'm trying to change the values in. And changing tableView.reload() to self!.fieldTableView.reload() doesn't work either.

From 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)
        }

Edit The part of the code that should have worked is this:

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

Upvotes: 1

Views: 2827

Answers (4)

Paul Osinga
Paul Osinga

Reputation: 17

Work for me :

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()
    })
})

Upvotes: 0

iAnurag
iAnurag

Reputation: 9356

This line causing malfunction

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

see carefully what you r writing in this condition you are appending data only ablove condition satisfies however in this case in your didselectrowatindexpath

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

it get fails thats why no data is getting appended when u r editing row in section 2 & 3 move appending code somewhere else or use if indexPath.row == 0 && indexPath.section == 1 this condition sectionwise

Upvotes: 1

Vito Royeca
Vito Royeca

Reputation: 657

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
        // ...
    })
})

Upvotes: 1

Vito Royeca
Vito Royeca

Reputation: 657

Use this instead:

tableView.reloadData()

Upvotes: 0

Related Questions