gpt4 book ai didi

ios - TableView 取数据时向下移动

转载 作者:行者123 更新时间:2023-11-28 14:55:09 24 4
gpt4 key购买 nike

当我几乎滚动到当前 TableView 的末尾时,它加载了更多数据,问题是在 reloadData() 之后它几乎立即移动到另一个单元格。例如,如果我在第 12 个单元格上停止滚动,tableView 将移动到第 15 个单元格。与 22 和 25 等相同。我不希望我的 tableView 跳过单元格。我该如何修复它?

我如何检查是否是加载更多数据的时间:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "previewCell") as! postsCell

let photosForFirstPost = photoURLs[indexPath.row + 1]
//print(photoURLs[0])

if photosForFirstPost?.count != 0 && photosForFirstPost != nil {


let url = photosForFirstPost![0]
// cell.imagePreview.kf.setImage(with: url)
let resource = ImageResource(downloadURL: photosForFirstPost![0], cacheKey: String(describing: photosForFirstPost![0]))
// print(ImageCache.default.isImageCached(forKey: String(describing: photosForFirstPost![0])))
cell.imagePreview.kf.setImage(with: resource)
} else {
cell.imagePreview.image = UIImage(named: "harry")
}
cell.separatorLine.backgroundColor = .blue
cell.themeName.text = "Theme name"
cell.readTime.text = "3 mins read"

cell.textPreview.text = realPosts[indexPath.row].text

if postsTableView.indexPathsForVisibleRows?.first?.row == realPosts.count - 3 {
var arrayOfIndexes = [IndexPath]()
for i in 0...9 {
arrayOfIndexes.append(IndexPath(row: realPosts.count + i, section: 0))
}
requestTenPosts(indexPath: arrayOfIndexes)
}

return cell
}

我如何在启动时请求数据:

func requestForPosts() {
guard let requestURL = URL(string: "https://api.vk.com/method/wall.get?owner_id=\(groupId)&count=\(howMuchPosts)&access_token=\(serviceKey)&v=\(versionOfMethod)&offset=\(offset)") else { return }

do {
self.posts = [try Welcome(fromURL: requestURL)]
realPosts = self.posts[0].response.items

searchPhotos(arrayOfItems: self.realPosts)
offset += howMuchPosts
} catch {
print(error)
}

}

我如何请求更多数据:

func requestTenPosts(indexPath: [IndexPath]) {
guard let requestURL = URL(string: "https://api.vk.com/method/wall.get?owner_id=\(groupId)&count=\(10)&access_token=\(serviceKey)&v=\(versionOfMethod)&offset=\(offset)") else { return }
DispatchQueue.global().async {

do {

self.offset += 10
for howMuchKeysToADD in (self.offset...self.offset + 10) {
self.textOfAPost.updateValue("", forKey: howMuchKeysToADD)
self.photoURLs.updateValue([], forKey: howMuchKeysToADD)
}

var forAMoment = try Welcome(fromURL: requestURL)
var arrayForAMoment: [Item] = []

for i in 0...9 {
self.realPosts.append(forAMoment.response.items[i])
arrayForAMoment.append(forAMoment.response.items[i])
}

print(arrayForAMoment)

print("searchPhotos is called")
self.searchPhotos(arrayOfItems: arrayForAMoment)

DispatchQueue.main.async {
self.postsTableView.reloadData()
}

} catch {
print(error)
}

最佳答案

if postsTableView.indexPathsForVisibleRows?.first?.row == realPosts.count - 3 {
var arrayOfIndexes = [IndexPath]()
for i in 0...9 {
arrayOfIndexes.append(IndexPath(row: realPosts.count + i, section: 0))
}
requestTenPosts(indexPath: arrayOfIndexes)
}else{return}

或者使用简单的 else{} 并且不要忘记给 cell.tag = [indexpath.row]

关于ios - TableView 取数据时向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459759/

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