获取数据时,TableView会向下移动

时间:2018-03-23 23:53:56

标签: ios swift uitableview

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

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

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

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

    // На новых фотках это дерьмо почему-то nil
    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)
        }

2 个答案:

答案 0 :(得分:0)

您可以尝试在底部插入行,而不是重新加载数据。由于您已经可以访问所有index paths,因此不应该很难,table view也不应该移动。

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

    self.postsTableView.beginUpdates()
    self.postsTableView.insertRows(at: indexPath, with: .left)
    self.postsTableView.endUpdates()
}

答案 1 :(得分:0)

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}

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

相关问题