How to Reload Data on a Tableview without Losing Previous Data (Swift)

Asked

Viewed 460 times

0

Hello, I have a tableview that will receive data on demand. however when I call scrollViewDidEndDragging function it has a call to tableview.reloadData() it replaces 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 dice

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

}

2 answers

-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 observe each time you reload, the data source is overwritten. This considering that self.tablelist is the data source of tableView.

Hint: I usually create an object and the data source becomes an array of that object, as it will help anyone working on the code and give a context to their data. Follow 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 it helped.

-1


Hey, I got it this way. I used Nsmutablearray as main base but instead of using addObject I used meuArray.addObjectsFromArray(meuArray)

With this at the time of picking the values of the Array I can use the Nsmutabledictionary was worth there staff.

att,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.