gpt4 book ai didi

ios - UITableView 重新加载问题

转载 作者:行者123 更新时间:2023-11-28 07:38:20 25 4
gpt4 key购买 nike

我在 UIScrollView 中有 UITableView 并实现了分页,每页有 10 条记录。当 IndexPath 行为 9 之后 UITableView 再次重新加载从第 2 行开始的单元格时,我遇到了一个问题,因此所有页面都加载了一次。这是我的代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if listData != nil{
print("list count in numberOfRowsInSection\(listData?.count)")
return (listData?.count)!
}
return 0

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("CellForRowIndexPath:\(indexPath.row)")
let cell: NewPostCell = tableView.dequeueReusableCell(withIdentifier: "NewPostCell") as? NewPostCell ??
NewPostCell(style: .default, reuseIdentifier: "NewPostCell")
cell.delegate = self
cell.updateWithModel(self.listData![indexPath.row] as AnyObject)
cell.tbleUpdateDelegate = self
cell.selectionStyle = .none
cell.accessoryType = .none
return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("in will display row - \(indexPath.row)")

if pageNumber >= totalPages {
return
}
else
{
if (listData?.count)! == 10*pageNumber{
if (listData?.count)! - 3 == indexPath.row{
if !boolHitApi{
boolHitApi = true
return
}
pageNumber += 1
self.callService()
}
}
}
}

override func viewWillAppear(_ animated: Bool) {
callService()
}
func callService(){
SVProgressHUD.show()
setPageToOne()
ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId: UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(result) in
SVProgressHUD.dismiss()
self.totalPages = result.totalPages!
if self.listData?.count == 0 || (self.pageNumber as AnyObject) as! Int == (1 as AnyObject) as! Int{
self.listData = result.userdata?.newPostData
} else {
self.listData = self.listData! + (result.userdata?.newPostData)!
}
self.tableView.reloadData()
})
}

最佳答案

您在评论中提到的代码向数据源 (self.listData) 添加了 10 行,但您只调用了一行的 insertRows。

您可以遍历它们,向数组添加一个项目并每次添加一行:

func callService() {
ProfileApiStore.shared.requestToGetProfile(loggedInUserId: UserStore.shared.userId, userId: UserStore.shared.userIdToViewProfile, limit: "10", page: String(self.pageNumber), completion: {(result) in
SVProgressHUD.dismiss()
self.totalPages = result.totalPages!
let newItems = result.userdata?.newPostData

tableView.beingUpdates()
for item in newItems {
self.listData.append(item)
let indexPath = IndexPath(row:(self.listData!.count-1), section:0) // shouldn't this be just self.listData!.count as your adding a new row?
tableView.insertRows(at: [indexPath], with: .left)
}
tableView.endUpdates()
})
}

关于ios - UITableView 重新加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52869746/

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