[英]swift keep tableview data position after reload
add new data to the tableview
when the scroll is above 50% of the content, but the problem is that after adding the scroll moves with the content to the top, how can I save the location and scroll ? 当滚动超过内容的50%时,将新数据添加到
tableview
,但是问题是,在将滚动与内容一起移动到顶部之后,如何保存位置并滚动? changed the display contents of the table that would start from the bottom 更改了从底部开始的表的显示内容
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if (self.lastContentOffset > scrollView.contentOffset.y && loadEarlierShowF) {
let height = scrollView.frame.size.height
let cHeight = scrollView.contentSize.height
let contentYoffset = scrollView.contentOffset.y
let distanceFromBottom = scrollView.contentSize.height - contentYoffset
print(distanceFromBottom)
let eght = cHeight / 100 * 50
if distanceFromBottom >= eght {
actionLoadEarlier()
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.row == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCSectionHeaderCell", for: indexPath) as! RCSectionHeaderCell
cell.bindData(indexPath, messagesView: self)
return cell
}
if (indexPath.row == 1) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCBubbleHeaderCell", for: indexPath) as! RCBubbleHeaderCell
cell.bindData(indexPath, messagesView: self)
return cell
}
if (indexPath.row == 2) {
let rcmessage = self.rcmessage(indexPath)
if (rcmessage.type == RC_TYPE_STATUS) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCStatusCell", for: indexPath) as! RCStatusCell
cell.bindData(indexPath, messagesView: self)
return cell
}
if (rcmessage.type == RC_TYPE_TEXT) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCTextMessageCell", for: indexPath) as! RCTextMessageCell
cell.bindData(indexPath, messagesView: self)
let numSections = self.tableView.numberOfSections
if numSections == 1 {
updateTableContentInset()
}
return cell
}
}
if (indexPath.row == 3) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCBubbleFooterCell", for: indexPath) as! RCBubbleFooterCell
cell.bindData(indexPath, messagesView: self)
return cell
}
if (indexPath.row == 4) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCSectionFooterCell", for: indexPath) as! RCSectionFooterCell
cell.bindData(indexPath, messagesView: self)
return cell
}
return UITableViewCell()
}
func updateTableContentInset() {
let numSections = self.tableView.numberOfSections
var contentInsetTop = self.tableView.bounds.size.height
for section in 0..<numSections {
let numRows = self.tableView.numberOfRows(inSection: section)
let sectionHeaderHeight = self.tableView.rectForHeader(inSection: section).size.height
let sectionFooterHeight = self.tableView.rectForFooter(inSection: section).size.height
contentInsetTop -= sectionHeaderHeight + sectionFooterHeight
for i in 0..<numRows {
let rowHeight = self.tableView.rectForRow(at: IndexPath(item: i, section: section)).size.height
contentInsetTop -= rowHeight
if contentInsetTop <= 0 {
contentInsetTop = 0
break
}
}
if contentInsetTop == 0 {
break
}
}
self.scrollView.contentInsetAdjustmentBehavior = .never
self.tableView.contentInsetAdjustmentBehavior = .never
self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0)
}
how to do that would be adding new data and contents of the scroll data remained in place (in example telegram) 该怎么做,将添加新数据并将滚动数据的内容保留在原处(例如电报)
Has tried so, but is shifting not there 曾经尝试过,但没有转移
let offset = tableView.contentOffset
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.setContentOffset(offset, animated: false)
....
let indexPath = IndexPath(row: 2, section: tableView.numberOfSections - 1)
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.scrollToRow(at: indexPath, at: .top, animated: false)
Why you can try this? 为什么您可以尝试呢?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableTest.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = arrayTemp[indexPath.row]
if indexPath.row > (arrayTemp.count / 2) {
callNewData()
}
return cell
}
You can use a index loaded in table to add more values. 您可以使用表中加载的索引来添加更多值。
when a cell is loaded you can call the function to add more data to table view, so if the data started with 10 values when a 5 cell is loaded you add more 10 itens in array temp so now the array have 20 values, when the table load a index 10 you call more itens. 当加载单元格时,您可以调用该函数以向表视图添加更多数据,因此,如果在加载5个单元格时数据以10个值开头,则在数组temp中添加更多10个itens,因此当表加载一个索引10,您称之为更多itens。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.