15

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.

3 Answers 3

34

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

1
  • This doesn't refresh the tableview as if you were to restart the app. What is totally refreshing as if app was started and tableview was freshly created? Feb 1, 2014 at 21:14
9

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

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.