滚动ios swift时刷新表

时间:2014-08-26 08:34:04

标签: ios swift tableview

我有自定义单元格的tableView。我从互联网上加载一些信息,如果用户向下滚动表格,则将其添加到表格中。问题:加载信息后,新单元格有时会重写旧单元格,有时在滚动时会在此表格中显示空白单元格。有我的代码:

var Bool canEdit = true
@IBOutlet var table : UITableView!
var eventList : [EventCell] = [] // EventCell - custom cell class

func addEventsAndRefresh() {
   if (canEdit){        
       canEdit = false
       HTTPManager.getEvents(isFuture: true, offset: eventList.count, didLoad: getEvents) //it creates url and call function "getEvents" after ending async request
   }    
}

func getEvents(response : NSURLResponse!, data : NSData!, error : NSError!) {
    if ((error) != nil) {
        HTTPManager.showErrorAlert()
    } else {
        var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSArray
        for elementOfArray in result {
            var cell : EventCell = table.dequeueReusableCellWithIdentifier("EventCell") as EventCell
            ... // parsing result to cell
            eventList.append(cell)

        }
        table.reloadData()
        canEdit = true
    }
}


func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return eventList.count
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    return eventList[indexPath.row]
}

func scrollViewDidEndDragging(scrollView: UIScrollView!, willDecelerate decelerate: Bool) {
    var currentOffset = scrollView.contentOffset.y;
    var maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    if (maximumOffset - currentOffset <= -40.0 && canEdit) {
        addEventsAndRefresh()
    }
}

example of blank screen

1 个答案:

答案 0 :(得分:1)

当我重复使用单元格变量时,我遇到了同样的问题。 尝试不将您的单元格放在列表中,而是将结果列表保存为实例变量,并通过cellForRowAtIndexPath函数中的索引获取对象,如下所示。

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
     var cell : EventCell = table.dequeueReusableCellWithIdentifier("EventCell") as EventCell
     var element = result[indexPath.row] as YourObject
     //Do something with cell
     return cell
}