gpt4 book ai didi

ios - 滚动 ios swift 时刷新表格

转载 作者:行者123 更新时间:2023-11-29 02:43:08 25 4
gpt4 key购买 nike

我有带有自定义单元格的 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

最佳答案

当我像你一样重用单元格变量时,我遇到了同样的问题。尽量不要将单元格放在列表中,而是将结果列表保存为实例变量,并通过 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
}

关于ios - 滚动 ios swift 时刷新表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25501365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com