Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 615
subjective-c Avatar asked Dec 07 '08 13:12

subjective-c


3 Answers

Use [tableView reloadData] - that will make the tableview run though the rows and rebuild itself.

like image 114
Adam Byram Avatar answered Oct 28 '22 10:10

Adam Byram


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

like image 24
millenomi Avatar answered Oct 28 '22 09:10

millenomi


I would add that if it is in a editing view that then saves some modified information stored in your table you should have a delegate that detects when the data was actually saved then reload your tableview. viewWillAppear will be fired every time your view appears. What I think you are after is only after data has been changed you want to refresh the table.

like image 32
Michael Avatar answered Oct 28 '22 08:10

Michael