如何在不丢失先前数据的情况下重新加载tableview中的数据(SWIFT)

时间:2015-03-18 02:52:10

标签: ios uitableview swift reloaddata

我有一个可以按需接收数据的tableview。但是,当我调用scrollViewDidEndDragging函数调用tableview.reloadData()时,它会替换先前加载的数据。

func carregaDados(pagina : Int)
{
    let pg : String = String(pagina)
    liberaLoad = false
    var url : String = "my rest json"
    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
        }
    })

}

这里我有一个附加数据的功能

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

我的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
}

1 个答案:

答案 0 :(得分:0)

要为单元格保存的数据,应考虑将其保存在自己的modelObjectsForCells中。在UITableViewCells中创建cellForRowAtIndexPath时,您将根据相应的modelObjectForCells创建单元格,并且当某个单元格中的数据发生更改时,您需要修改相应的modelObject。

这将确保您的UITableViewCell保留数据。