重载后swift保持tableview数据位置

问题描述 投票:1回答:1

当滚动超过内容的50%时,将新数据添加到tableview,但问题是添加滚动后将内容移动到顶部,如何保存位置并滚动?更改了从底部开始的表格的显示内容

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)

}

如何做到这一点将添加新的数据和滚动数据的内容保持在原位(例如电报)

已经尝试过,但不是在那里转移

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)
ios swift
1个回答
0
投票

为什么你可以尝试这个?

  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
}

您可以使用表中加载的索引来添加更多值。

加载单元格时,可以调用该函数向表视图添加更多数据,因此如果数据在加载5单元格时以10个值开始,则在数组temp中添加10个itens,这样现在数组有20个值, table加载索引10你调用更多itens。

© www.soinside.com 2019 - 2024. All rights reserved.