How to reload data in a tableview without losing previous data (swift)

0

Hello, I have a tableview that will receive data on demand. however when I call the scrollViewDidEndDragging function that has a call to the tableview.reloadData () it overrides the previously loaded data.

Load Data

func carregaDados(pagina : Int)
{
    let pg : String = String(pagina)
    liberaLoad = false
    var url : String = "minha url json rest"
    var request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: url)
    request.HTTPMethod = "GET"
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler: { (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
        let jsonResult : NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
        if(jsonResult != nil){
            //process json
            let jsonUnico: NSMutableArray! = jsonResult["lista"] as? NSMutableArray
            self.tableList = jsonUnico
            self.tableView.reloadData()
            self.liberaLoad = true
        }else{
            //nao foi possivel ler o json
        }

    })     
}

Here I have the function that was to give an append in the data

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    let currentOffset : CGFloat = scrollView.contentOffset.y;
    let maximumOffset : CGFloat = scrollView.contentSize.height - scrollView.frame.size.height;
    if (maximumOffset - currentOffset <= -60.0 && liberaLoad) {
        self.carregaDados(pagina:2)
    }
}

Here my cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustonImageTableViewCell
    let reeditList : NSMutableDictionary = self.tableList[indexPath.row] as NSMutableDictionary

    let tipo = reeditList["tipo"] as? String
    cell.lblTipo.text = tipo
    return cell

}
    
asked by anonymous 18.03.2015 / 03:40

2 answers

-1

Hello, I have succeeded in this way. I used the main base NSMutableArray instead instead of using addObject I used myArray.addObjectsFromArray (myArray)

With this in the moment of picking up the values of the Array I can use the NSMutableDictionary, thanks to the staff.

att,

    
19.03.2015 / 19:19
-1

Hello, the problem is here:

Swift Code:

   if(jsonResult != nil){
        //process json
        let jsonUnico: NSMutableArray! = jsonResult["lista"] as? NSMutableArray
        self.tableList = jsonUnico //<-
        self.tableView.reloadData()
        self.liberaLoad = true
    }else{

If you notice each time you reload, the data source is overwritten. This considering that self.tablelist is the data source of tableView.

Tip: I usually create an object and the data source is an array of this object, as it will help anyone who works on the code as well as giving context to their data. Here's an example of what I mean.

//Livro.Swift
class Livro:NSObject {
    var nome = ""

    init(nome: String) {
        self.nome = nome
    }
}

//ViewController que possui a TableView
var fonteDados = [Livro]()

//Quando for buscar por mais livros. 
//Essa é a maneira mais segura! Nunca se sabe o que vem do servidor
if let livros = jsonResult["LIVROS"] as? NSArray {
    for livro in livros {
        if let nome = livro as? String {
            fonteDados.append(Livro(nome: nome))
        }
    }
}

//Para preencher a tableView com "Livros"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //...
    linha.textLabel?.text = fonteDados[indexPath.row].nome
    //...
}

I hope you have helped.

    
19.03.2015 / 22:12