wayneh
wayneh

Reputation: 4413

Where is the proper place to reload TableView data?

My project has a few view controllers, let's say A and B. In A I have a UITableView. When a row is selected, I pass the row number and cell text to view B, then pushViewController B.

In view B, when hitting the 'done' button I make changes to the underlying data (right now a Singleton array) and then pop back to view A. In view A viewDidAppear, I reload the UITableView to see the new data.

Is this the best way to do it? Or should I be reloading the UITableView when the 'done' button is hit? If so, how do I reload a Table in view A from within view B?

Thanks.

Upvotes: 0

Views: 123

Answers (4)

AJ.
AJ.

Reputation: 174

Instead of using NSNotification. You can assign view A to be view B's delegate which is a much cleaner design pattern.

So in view A it would look like

ViewB *b = [[ViewB alloc] init];
b.delegate = self;

and the in viewB when you are done making modification u call

[delegate updateTable];

where in updateTable is a @required method in the delegate declaration and since its required viewA will need to implement it if it says it conforms to that delegation.

Also this method will be called before you pop the viewB out.

Upvotes: 0

aqs
aqs

Reputation: 5682

There is nothing very wrong about this, except that your table will be reloaded every time it's viewDidAppear is called, which may not be very good depending upon the size of the table. The best way would be to use @protocol and make a quick delegate design pattern, though this might seem unnecessary headache if you are not used to it. You can also use NSNotifications, although I would not insist using it in this case, as there are not many observers listening to the event.

Upvotes: 0

picciano
picciano

Reputation: 22701

ViewController "A" should certainly be responsible for deciding to reload the table data. I would consider putting that into the "viewWillAppear" method of ViewController A. That way the table is reloaded before being displayed to prevent and "flickering".

Upvotes: 0

antf
antf

Reputation: 3222

It is better to reload the data directly when you hit the "Done" button. You can do that by a Notification Center. Please check this link for more details about Notification Center.

Upvotes: 2

Related Questions