Update UITableView While Looking at View? - iphone

I am currently building a UITableView that reads an array and displays it on a TableView. I am continually adding to the array I would like to have a button on the view that once is clicked it updates the UITableView.
How do I implement a reload of the UITableView from the new array I just built?

If you are adding and removing items one to a few at a time you will likely want to implement the animated table view methods for insertion and deletion.
When you add or remove an item from the table view you will prepare by calling [tableView beginUpdates] make your array modifications and then call [tableView endUpdates]
Between the begin and end update calls you will perform the actually deletion/insertion.
For deletion you should use
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
Notice UITableViewRowAnimationBottom you will want to play around with this to get the best animation depending on the position of the element. Also iOS5 has a enum value something like UITableViewRowAnimationAutomatic and it will use the best option.
For insertion you should use
insertRowsAtIndexPaths:withRowAnimation: the same way I show above with delete.
For more info check out the section with these methods listed in the UITableView class reference

The function you're looking for is [self.tableView reloadData];. Here's a link to the Apple documentation for UITableView.

Related

updating tableViewCell with modifications done in another view

In my app am using tableview in which each cell am adding UIScrollView, in that scrollView again am adding UIView(this view is adding from another view class). When touch actions of view is performing but when i scroll the table all the modifications in view is reloading with what is there at the time of loading.
So how can we save the modifications of view which are done in another class in present tablecell.
in touchesEnded method am using
[music.createTableView beginUpdates];
[music.createTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[music.createTableView endUpdates];
Any help or suggestion.
Thanks in advance.
Suppose, you are using NSArray to show data in rows of tableview. Inside that, you will keep another NSArray to show array of UIViews inside a every row. If you use NSDictionary to store data of UIView, you can set a flag in that dictionary which will change according to user touch. Hope it will help you.

Update UITableViewCell without reload

So here's my situation:
I have a UITableView where each cell has some buttons.
In the cellForRowAtIndexPath I set the indexpath of the row so when the a button is clicked I know what row was clicked.
One of the buttons is a delete button and when pressed I remove the backing data and delete the row from the table.
The problem I'm having is that after I delete a row the indexPath's of the other rows are incorrect as the other rows have not been redrawn. The only way I can find to redraw the table is to call reload but this messes up the animation.
I figure this kind of problem must have been addressed before. Does anyone have any suggestions on how to get around this? I'm ok with changing the way I've designed my buttons to work if there is a better way.
Think you need to use this...
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Apple reference!
You should be able to redraw the tableview without a full reload by starting and ending an update like this:
[tableView beginUpdates];
[tableView endUpdates];
call the method:
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
It is better not to store the indexPath with the buttons. In the button action handler (which is either a method in your cell subclass or in your viewController) you could identify the cell, and subsequently determine the indexPath using -indexPathForCell:.

How can I force a UITableView to reloadData while keeping a UISearchBar active?

I've got a UITableView that has 20 sections. At the top I have a UISearchBar, and I want to filter the sections live as the user types.
Unfortunately, if the UISearchBar is active and if I return NO from searchBarShouldEndEditing: then my [tableView reloadData] call is ignored. If I return YES from searchBarShouldEndEditing: then the reloadData call works fine but I lose firstResponder after each character typed.
How can I force the UITableView to do live updates and filtering without having to resignFirstResponder on the UISearchBar between each character typed?
I faced the same problem and ended up with a quite elegant solution :
You place your search bar in a specific section of your table (let's say index 0). You place your table data in another section (let's say index 1).
When the text of your search bar changes, you can update your model and then simply call :
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
This way, your keyboard will still be active, your search bar will still be the first responder, and you will benefit from nice built-in table animations !
You could save yourself a lot of work by using the UISearchDisplayController and just feeding it the same datasource. It manages the search bar and its own table view for displaying filtered results.
I had similar problem. It turned out that it had to do with animations I used when table was reloading data. When I removed reloadSections:withRowAnimation, and simply called:
[self.tableView reloadData];
instead of calling fancier methods with animations for refreshing the table data, the search bar did not resign first responder on any key entered anymore.
Hope this helps...

Why my tableView doesn't update without scrolling it down?

I am making an nav based app. in my table view i am parsing a xml and showing its data. when i enter new feed item in my xml and click refresh button, my data updates but it didn't appear in tableView untill i scroll it down. When i scroll it down it appears. Can anybody tell me why its happening like that? I want new data appear when i enter the feed item without scrolling my tableView down. Thanx for help
you need to call [self.tableView reloadData] to update the tableView with new contents
Did you call [tableView reloadData] when the data has been refreshed?
You need to reload the cells (or the entire table) for the new data to appear.
Depending on if you return parsed results right away or all results after the entire xml-file has been parsed.
The code to update the table is:
[myTableView reloadData];
You have to call [tableView reloadData] to refresh the data from the UITableView.
Call [tableView reloadData] when the parsing is completed
I'm not exactly sure what you mean. A simple reload is:
[myTableView reloadData]
in the UITableViwController
[self.tableView reloadData]
If you scroll down, new cells that are rendered will use the new data. but cells that are on display have to be told to update immediatly.
Not just -reloadData, you can use any of these:
reloadData
reloadRowsAtIndexPaths:withRowAnimation:
reloadSections:withRowAnimation:
reloadSectionIndexTitles
Basically, you'll need to tell your tableview that your data has changed so it knows to change that. You can do it with fancy animations and whatnot so that the user isn't confused when what's displayed on screen suddenly changes.

Redoing UITableView layout when user taps Edit button?

I have a UITableView with complex content. The user can edit (rearrange and delete) the cells when tapping the Edit button the standard way. But I want the cells to look different in "edit" mode.
Question:
How to change the UITableView Layout in edit mode, including changing row height?
So far, this is what I have:
The Edit button sends a WillTransitionToState/DidTransitionToState message to each uitableviewcell (UITVC). I have subclassed UITVC and react to these inside each cell, hiding and removing and reshuffling as needed. But, changing the row height is beyond the scope of one cell.
There does not seem to be a message sent to UITableView when user taps edit. There is a - tableView:commitEditingStyle:forRowAtIndexPath: sent to data source after editing a particular row.
Inside heightForRowAtIndexPath, I can query the current mode using the tableView.editing property, and report height as appropriate. And I can trigger re-flowing the table, including recomputing the heights, by invoking [tableView reloadData]. But, when do I call it?
I could send messages from the cells from within WillTransitionToState back to the "owning" table view, and call reloadData when I get them. But this sounds fragile and there must be a better way.
Rhythmic is right. Using reloadData kills the nice editing animation.
This problem is addressed in this post:
Can you animate a height change on a UITableViewCell when selected?
Instead of using reloadData, do the following after calling setEditing:animated.
[tableview setEditing:editing animated:YES];
[tableview beginUpdates];
[tableview endUpdates];
If you wish for your table cells to change their format in response to whether or not the table is in editing mode, you could override -setEditing:animated: in your UITableViewController and trigger a reload (via -reloadData) of the table view on a change of editing state.
Within your UITableViewController's -tableView:cellForRowAtIndexPath: method, you could check for whether or not the table was in the editing state by querying the editing property on the table view, and then return a different cell type depending on which state the table is in.