简体   繁体   中英

unwinding segue, need to refresh tableview

I have looked all over for this problem and I cannot find an answer.

So on the first page of my app I have a page in a navigation controller. Within that page is a tableview. Within the cells of the table, there is a button. For the view controller of the cells, I say remove the button from superview if user is not signed in. So when you first load the app, there is no button.

Now if you click the sign in button (a button at the top of the page), it goes to that page, then there's two possible things that can happen. At first what I was doing was making a segue back to the main page. Everything worked perfectly fine. The button appears in the cells.

But I don't like how a second copy of the main page is being added to the stack of navigation controllers. So what I have explored with now is the popnavigationcontroller method and the unwind segue. So they both work but the problem is when it unwinds back to the new page, the button I mentioned earlier does not appear in the cells, even though its supposed to appear since the user is signed in. The reason for this is because the method "dequeueReusuableCell" is using the old cells, in which I have removed the button. And now that the button is removed, there is no easy way for me to add it back in at the right place in the view.

So what is the best solution here? At first I was looking for a way to "clear" all the reusuablecells for a tableView but I'm not sure if I found an answer. What I would like to do is when I rewind back to that navigation controller, refresh everything like it was the first time ever seeing it.

I guess another solution I could do is to perform a normal segue from the sign in page to the main page, and then remove the previous redundant main navigation controller. Is there a better solution?

When your controller pops off the stack, the underlying view controller will have its viewDidAppear(animated:) function called. So you could have:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    tableView.reloadData()
}

This is assuming based on your post that the issue is only about reloading your table. If you reload the table and your cells are still not updating properly, you have an issue with your dataSource not configuring the cells properly. But I cannot tell if this is the case without any code.

Edit: this was a cell reuse issue. Cell configuation had some code to remove a subview in some instances, but never add the subview back if it was supposed to be there. The simplest fix is to use isHidden instead of outright removing the subview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM