【问题标题】:how to make table view shows specific row after reloading the data in swift?如何在快速重新加载数据后使表格视图显示特定行?
【发布时间】:2019-01-12 22:46:19
【问题描述】:

我正在尝试在表格视图中实现分页。所以从服务器获取数据时,我希望每个请求获取 10 个数据。

所以用户第一次打开视图会获取10条数据,用户滚动到第10行后,会发送请求获取第11-20行的数据。

但在获取第 11-20 条数据并重新加载表格视图后,表格视图似乎向下拖动并显示最后一行(即第 20 行)。

我要在第 10 行之后 --> 获取数据 --> 显示表格视图的第 11 行

所以它会提供流畅的滚动和良好的用户体验

这是我使用的简化代码。提前致谢

    let numberOfDocumentsPerQuery = 5
    var fetchingMore = false
    var recommendedEventList = [EventKM]()
    var lastDocumentFromQuery : QueryDocumentSnapshot?

    extension HomeVC : UITableViewDelegate, UITableViewDataSource {
        //MARK: - Table View Methods

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return recommendedEventList.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeCell
            cell.eventData = recommendedEventList[indexPath.row]
            return cell
        }


        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

            // to fetch more events if it comes to the bottom of table view

            let currentOffset = scrollView.contentOffset.y
            let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

            if maximumOffset - currentOffset <= 10.0 {
                if !fetchingMore {
                    getRecommendedEvents()
                }
            }
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "toEventDetailVC", sender: nil)
        }

    }

 func getRecommendedEventsFromBeginning() {

        EventKM.observeRecommendedEvents (

        selectedCity: selectedCity,
        limit: numberOfDocumentsPerQuery) { (recommendedList, listener, lastDocument) in

            if let events = recommendedList {
                self.recommendedEventList = events
                self.tableView.reloadData()

                self.lastDocumentFromQuery = lastDocument
                self.recommendedListener = listener
                self.fetchingMore = false

            } else {
                self.fetchingMore = true // to stop fetching data
            }


        }

    }

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:
// Add your custom object.
recommendedEventList.add(customObject)

let numberOfRowsBeforeAPIRequest = tableView.numberOfRows(inSection: section)
let indexPath:IndexPath = IndexPath(row:(recommendedEventList.count - 1), section:0)
tableView.insertRows(at:[indexPath], with: .none)

// Do Validate if the actual indexpath exists or not, else will crash.
let ip = NSIndexPath(forRow: numberOfRowsBeforeAPIRequest + 1, inSection: 0)

tableView.scrollToRowAtIndexPath(ip, atScrollPosition: .bottom, animated: true) 

只是把我想做的事情放在上面。

  1. 在重新加载之前保存行数。
  2. 在该部分的最后一行之后添加新行。
  3. 然后在表格视图中向下滚动 1 行。

【讨论】:

    【解决方案2】:

    你可以有一个属性来记录滚动到的行。
    var rowForScroll = 1

    每次加载数据时,添加rowForScroll

    rowForScroll = rowForScroll + 10
    let ip = NSIndexPath(forRow: rowForScroll, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(ip, atScrollPosition: .bottom, animated: true)
    

    【讨论】:

    • 我在哪里实现这个?在 tableView.reloadData() 之后?
    • 是的,在重新加载数据之后。
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2014-11-13
    • 2015-01-02
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多