gpt4 book ai didi

swift - Tableview 插入行跳动

转载 作者:行者123 更新时间:2023-11-30 11:45:31 24 4
gpt4 key购买 nike

我已经在 TableView 中实现了 insertRows 方法。如果我在 tableView 插入新行时向下滚动或滚动到 tableView 内容的底部,则整个表格会发生跳跃并将我置于 tableview 内容中的不同位置。我该如何解决?我已在底部添加了我的代码:

//Method to load more data
func loadMoreFeed() {
let rowsBefore = self.tableObjects.count
loadMoreFeedInBackground() { queryObjects in
for object in queryObjects! {
self.tableObjects.append(object)
}

self.tableObjects.sort {
$0.date!.compare($1.date!) == .orderedDescending
}

let rowsAfter = self.tableObjects.count
var indexPaths = [IndexPath]()

for i in 0 ... (rowsAfter - rowsBefore - 1){ //start i at zero and go up to the difference - 1
indexPaths.append(IndexPath(row: rowsBefore + i, section: 0)) //insert an index path in the array
}

self.isMoreDataLoading = false
self.loadingMoreView!.stopAnimating()

self.tableView.beginUpdates()
self.tableView.insertRows(at: indexPaths, with: .automatic)
self.tableView.endUpdates()
}
}

何时加载新对象的方法

var isMoreDataLoading = false
var loadingMoreView : InfiniteScrollActivityView?

/* Loads more feed while scrolling */
override func scrollViewDidScroll(_ scrollView: UIScrollView) {

if (!isMoreDataLoading) { //is not loading more

// Calculate the position of one screen length before the bottom of the tableview
let scrollViewContentHeight = tableView.contentSize.height
let scrollOffsetThreshold = scrollViewContentHeight - tableView.bounds.size.height

//start loading more data after user went 4/5 of the way through the content
if(scrollView.contentOffset.y > 4*scrollOffsetThreshold/5 && tableView.isDragging) {

isMoreDataLoading = true //set loading data to true

// Update position of loadingMoreView, and start loading indicator
let frame = CGRect(x: 0, y: tableView.contentSize.height, width: tableView.bounds.size.width, height: InfiniteScrollActivityView.defaultHeight)
loadingMoreView?.frame = frame
loadingMoreView!.startAnimating()
loadMoreFeed()

}
}
}

最佳答案

对于何时加载新对象的方法,这是我的实现

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {    
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
// print("this is the last cell")

if (!isMoreDataLoading) { //is not loading more
// start loading indicator
loadMoreFeed()

// Update position of loadingMoreView, and
loadingMoreView?.frame = CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: InfiniteScrollActivityView.defaultHeight)
loadingMoreView!.startAnimating()

self.tableView.tableFooterView = loadingMoreView
self.tableView.tableFooterView?.isHidden = false
}
}
}


为了更新 tableView 这是我的实现

private func updateTableView() {
let rowsBefore = self.tableObjects.count
loadMoreFeedInBackground() { queryObjects in
for object in queryObjects! {
self.tableObjects.append(object)
}

self.tableObjects.sort {
$0.date!.compare($1.date!) == .orderedDescending
}

let rowsAfter = self.tableObjects.count
let indexes = rowsAfter - rowsBefore

guard indexes > 0 else {
self.isMoreDataLoading = false
self.loadingMoreView!.stopAnimating()
self.tableView.tableFooterView?.isHidden = true
return
}

var indexPaths = [IndexPath]()
for index in 0...(indexes - 1) {
let indexPath = IndexPath(row: index + rowsBefore, section: 0)
indexPaths.append(indexPath)
}

if tableView.contentOffset.y > tableView.estimatedRowHeight {
let initialOffset = self.tableView.contentOffset

tableView.beginUpdates()
tableView.insertRows(at: indexPaths, with: .fade)
tableView.setContentOffset(initialOffset, animated: false)
tableView.endUpdates()

} else {
tableView.insertRows(at: indexPaths, with: .fade)
}

//refreshControl.endRefreshing()
}

关于swift - Tableview 插入行跳动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873986/

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