how to make table view shows specific row after reloading the data in swift?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



how to make table view shows specific row after reloading the data in swift?



I am trying to implement pagination in table view. so when fetching data from server, I want to get 10 data per request.



so the first time user opens the view, it will fetch 10 data, after the user scrolls to the 10th row, it will send request to get the data 11st to 20th.



but after fetching that 11-20th data and reloading the table view, it seems the table view drag down and it shows the last row (i.e the 20th row).



I want after the 10th row --> fetching data --> show the 11st row of the table view



so it will give smooth scrolling and good user experience



here is the simplified code i use. Thanks in advance


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










check this stackoverflow.com/questions/5856427/… scroll your tableview to specific position
– Shauket Sheikh
Aug 6 at 2:13




2 Answers
2



You can have a property to record the row that scroll to.
var rowForScroll = 1


var rowForScroll = 1



every time you loaded data, add the rowForScroll


rowForScroll


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





where do I implement this ? after tableView.reloadData() ?
– sarah
Aug 6 at 2:36





Yep, after reload data.
– NF Lee
Aug 6 at 2:40


// 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)



Just putting what I am trying to do above.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS