Where is the proper place to reload TableView data? - iphone

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.

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.

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

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.

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.

Related

Is it possible to call a UIEvent between viewWillAppear and viewDidAppear?

Is it possible to handle UIEvents between viewWillAppear and viewWillDisappear?
For example:
Let's say I have a view controller with a UITableView.
When pressing a cell I push a another view controller to the navigation controller (And display it of course).
Is it possible that after the viewWillAppear event of the pushed view (And before the viewDidAppear) I can still press one of the cells in the UITableView?
I am asking this because I am seeing this in the logs of my application and I Never thought that such a thing could be possible..
What do you guys(Or girls) think?
Thanks!
I have one idea may this work,
you use performSelector in viewWillAppear with 0.01 or another time with Delay Parameter and call your method here,
hope,this help you.
:)

Split View Controller Data parsing on the iPad

I have following codition for my app as shown in image.
On left View : The different User selection will be done in table view.
And the selected users information will be needed on right view while pressing the button.
How can I do this ?
Use of Protocol is very simple to solve your problem.
When you tap on row on left side, then you just need to pass the object as userinfo and make a call to its delegate method which will be implemented in rightview.
This is very simple task if you know how to implement protocols for the viewController.
Hope this helps you.
Use a delegate pattern.
When a row is selected from the table view in the left view, call the delegate to notify the selection. Afterward your delegate (which suppose to be the right view's controller) can display the corresponding user info.

How to tell when a view has become active

I'm using a navigationController to push a new view onto the stack. Once I'm done with that view I pop it. I want the original or root view at the bottom of the stack which then becomes active to know when this has happened to it can call a method on itself
There are two options
1. implement -(void) viewWillAppear:animated: This is often a useful strategy for knowing that a view is about to become visible. If you are always doing the same thing when this happens, then this is quick and easy.
2. Send a NSNotification. This is useful when you want your underlying view to perform some action in response to a particular event happening elsewhere.
Use the viewWillAppear delegate. It is called right before showing the view.

How to programmatically set cell.textLabel.text from a different view?

I've got a view controller, call it VC1, that's a table view. When I tap a cell in the table view, I am presented with a new view controller, call it VC2, which is a short list of choices. After making a choice, I want to dismiss VC2 and set the cell.textLabel.text property of the VC1 cell I originally tapped to the value I selected in VC2.
Conceptually speaking, what is the proper way to do this? I've tried a handful of different approaches, but all of them seem wonky at best, and only one of them actually worked - although it was the most cumbersome of all, passing references to both view controllers and table view cells and all kinds of things. It just feels like I'm making a mountain out of what is probably a mole hill.
This is such a common paradigm that I find it hard to believe there's not a simple method for doing it.
There are a number of ways to handle this but one of the most flexible is via the Delegate Pattern. Define a delegate protocol in VC2's interface and then make VC1 conform to that protocol. When you create VC2 assign VC1 as it's delegate (similar to the way you did with your UITableView). One requirement of your protocol should be a method like didFinishWithStringSelection: (or whatever you call it) where you would update the table cell and reloadTable.
I do kind of that with with a table view presenting several attributes of an data object. To change a single attribute the user has to select the specific table cell showing the attribute he wants to change and that will push a new view controller (with a picker in my case) where the user can change the value from a selection.
I assign the data object to a property of the new controller before pushing. With this the value can be changed directly in the data object, and when i return (via navigation controller) to the first view controller there is a reloadData in viewWillAppear.
Did you try it this way?

What's the best way to refresh a UITableView within a UINavigationController hierarchy

I'm pretty new to iPhone development and have struggled to find what I consider to be a neat way around this problem.
I have a user interface where a summary of record data is displayed in a table inside a navigation controller. When the user clicks the accessory button for a row, a new view is pushed onto the navigation controller revealing a view where the user can edit the data in the corresponding record. Once done, the editing view is popped from the navigation controller's stack and the user is returned to the table view.
My problem is that when the user returns to the table view, the table still shows the state of the data before the record was edited. I must therefore reload the table data to show the changes.
It doesn't seem possible to reload the table data before it is displayed as the call only updates displayed records. Reloading it after the table has been displayed results in the old data changing before the user's eyes, which I'm not too happy with.
This seems to me like a pretty normal thing to want to do in an iPhone app.
Can anyone please suggest the best practice approach to doing this? I feel like I'm missing something.
Cheers - Steve.
The standard approach may sound like a lot of hassle at first, but is a useful pattern for a lot of situations.
In your tableview class create a method like:
-(void)editDone {
[self.tableView reloadData];
}
Add a property to your edit controller like:
#property (assign) id delegate;
Set the delegate when your accessory is clicked:
editController.delegate = self;
And when editing is complete, call your method like so:
[delegate performSelector:#selector(editDone) withObject:nil];
You can create similar methods to handle cancel of your edit component, or to carry out dismissing of modal edit controllers, etc. It's considered more classy to put all this in a protocol, if you like.
I'd implement this in the following way:
Save indexPath of a clicked cell.
Implement -[UIViewController viewWillAppear:] method of the view controller, which contains the UITableView. If saved indexPath is not nil, reload specified cells with:
-[UITableView reloadRowsAtIndexPaths:withRowAnimation:]