我在表视图中实现了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()
}
}
}
答案 0 :(得分:0)
for Method何时加载新对象这是我的实现
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()
}