0

i have a tabBar Controller, in segmented 0 i have a viewcontroller that has another view conttroller that also have a a tableview. i have to change from viewcontroller in segmentedIndex 1 when taping in a cell change to selecteIndex 0 like this

let appDelgate = UIApplication.shared.delegate as! AppDelegate
                // FIXME: - Update to real post id comming from Notification endpoint
                appDelgate.postID = 81

                self.tabBarController?.selectedIndex = 0

81 is just for testing purpose then in view controller that manage the selectedIndex 0 i have eset this in viewDidAppear

override func viewDidAppear(_ animated: Bool){
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    if let postID = appDelegate.postID, let data = feedFriends.feedVC.theData {
        self.pagerVC.selectedIndex = 0
        let i = data.index(where: { data in
            data.id == postID
        })
        print("i value: \(i)")
        let indexPath = IndexPath.init(row: i!, section: 0)
        feedFriends.feedVC.tableView.scrollToRow(at: indexPath , at: .top, animated: true)
        appDelegate.postID = nil
        print("el valor de post id \(String(describing: appDelegate.postID))")
    }
}

all this work as expected if this view have appear before make the transistion in the previos code. is i tap in the selected index 1 and tap a cell to show selectedIndex 0 and this is not be show, then the scroll did not happen do to let data = feedFriends.feedVC.theData in case this check is not done the i have a unespectely found nil because the theData is not loaded from network due to async task, so how do i do when the data has finished loaded to make the scroll to the desired index. i have seen this Auto scrolling to a cell with a specific value but I have solve the problem of scrolling to the specified value. so My question is how to know when to scrool after the tableview has loaded the data, beacuse the tableview does not have a delegate to inform that all data have been loaded so the tableview is goin to reload data, the diferences with the link is that i make the scroll to the desired cell when the tableview alredy have data, but if the all view is not been loaden from story board.

what function or protocol or delegate use to say the tableview hey ahve you loaded your data, then tableview.scrolltorow().

  • Possible duplicate of [Auto scrolling to a cell with a specific value](https://stackoverflow.com/questions/28043632/auto-scrolling-to-a-cell-with-a-specific-value) – Xcoder Nov 30 '17 at 22:24

1 Answers1

0

Have the view controller in whose viewDidAppear you wrote the above code act as a delegate to feedFriends.feedVC by conforming to a protocol that has a function telling the delegate that feedFriends.feedVC.theData is available. In that method, scroll the table view to the desired cell.

Santhosh R
  • 1,518
  • 2
  • 10
  • 14
  • ok i have move that code to the class acting as delegate for tableview but if the network task is not finished then i have this unexpectedly found nil while unwrapping an Optional value thats because theData is not yet avaible. so this would not make the scroll –  Dec 01 '17 at 01:51