Friday, March 4, 2011

How do I refresh a table view after returning from a "detail view?"

Hi,

I'm new to iPhone and Apple development and working on my first application. It simple with only a TableView and a "detail view" when an item in a table is selected.

What I want to do is change the background color of the cell in the TableView based on some action taken in my "detail view".

When the application initially loads I customize the colors in -cellForRowAtIndexPath: method, but when user navigates back from my detail view that function is not called, so my table view doesn't have the colors updated. The only way to get that refreshed now is to exit the application and start it up again. (I persist their selection with NSUserDefaults.)

Obviously, I want the table view to be refreshed when they come back form the detail view, but I don't know how to get a reference to a cell and in which method to do that. I'm assuming it should go in -viewDidAppear, since that is called everything the view is shown.

Thanks for your help! alex

From stackoverflow
  • You should really do that in viewWillAppear:.

    Also, you can get the visible cells of the table view by using its visibleCells method. Due to the way table views work, other cells do not exist at all -- scrolling will trigger the delegate method and create new cells (or dequeue existing ones if you use dequeue...).

  • Try [tableView reloadData] - that should make the tableview run though the rows and rebuild itself.

0 comments:

Post a Comment