Automatic deselection of table view cell after popping detail view? - iphone

I've been stumped by this for a while...
When you tap the cell of a contact in Apple's Phone app for the iPhone (entering the view with contact details) and tap back to the previous view, there is a brief animation (fading from blue to white) showing the deselection of the cell. This is the behavior with all table views in Apple's own apps, and also the recommended behavior according to their Human Interface Guidelines.
However, in my own project I've been having trouble replicating this behavior. None of the cells in my UITableView are selected when I return to it from a detail view.
I looked through the CoreDataBooks sample code in Apple's documentation, which has the desired cell deselection behavior, and it seems like the table view gets the behavior "automatically" (without any specific implementation).
I've also tried implementing the solutions in these very similar questions:
What deselect selected cell when navigationController pops a view?
iPhone UITableView cells stay selected
UITableView doesn't keep row selected upon return
But I always get the same result -- none of the cells in the UITableView are selected when I return to it. (Even after adding [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated]; in the view controller's -viewWillAppear: method.)
It was also suggested in the comments of one question that having [[self tableView] reloadData]; in -viewWillAppear: may be causing the cells not to stay selected. But CoreDataBooks does the same thing and still has the desired behavior (seemingly) without any specific code.
Any suggestions on how to resolve the problem? Thanks in advance.
On a side note, I don't quite understand why the code to deselect the cell should be implemented in -viewWillAppear: (rather than -viewDidAppear:). Wouldn't that cause everything to happen before the table view is displayed on screen? This is probably just due to lack of proper understanding of a view's life cycle on my part, but any clarifications would be nice. Thanks again.

Have you experimented with the clearsSelectionOnViewWillAppear property of a UITableViewController (not just a tableview but a tableviewcontroller)? If you set it to NO it will make the selection stay blue.
To get your desired effect,you will also probably need to add a call to
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
to ensure that it doesn't stay blue forever.
Look in the Overview section of the UITableView Documentation to get started.
//EDIT//
As noted in the comments below, what we finally ended up doing is moving the call to [[self tableview] reloadData]; to viewDidAppear.
My guess is that CoreDataBooks has a complex enough table (with sections and books and sections and books) that something is going on with the timing of events.

If you want to deselect a cell, use the - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated and use [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; or [tableView deselectRowAtIndexPath:indexPath animated:YES];.

Related

Proper way to implement a footer in UITableView

So I read here that I can simply drag a UITableViewCell to the bottom of my UITableViewController in storyboard and have it act like a footer. This footer row has an activity indicator in it that's it. It's width is 320 and height 55 with the indicator centered in the row. Note that it's a UIView rather than a UITableViewCell because I can only get it to work with the former.
First, The UITableView doesn't stop at the footer. One can see the footer if he extends his scrolling beyond the bottom of the UITableView. As soon as the user releases his finger, the footer disappears from site as the UITableView returns its scrolling back to the last element. I am trying to do what Instagram is doing - if you scroll to the bottom you can see their custom activity indicator at the bottom and the UITable will remain its scrolling position at that indicator. How can I achieve this?
Second, I have some custom scrolling performed by certain user actions. Right now, I have the following code:
if (row + 1 < [self.tableView numberOfRowsInSection:0]) {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:row+1 inSection:0]
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
} else {
// TODO scroll a little down so the user can see the activityIndicator
}
How can I tell my tableView to scroll programmatically to the footer and have it stop there?
Third During the very beginning, my UITableView has to fetch things from my server before it can populate the tableView. Unfortunately i can't get the footer view to maximize the space of the UITableView so that the activityIndicator will appear in the center. Right now this is how it looks:
I think I have the structs set correctly:
I suspect that having a UIView within a UITableView might prevent the view from maximizing.
Summary
Any recommendations on the above issues? I apologize for the length of this question, but I feel that they are all related to the same problem mentioned above.
I finally figured it out.
With regards to #zing and #Lithu answers, that worked. However, returning a footerView from this method forces the footerView to "stick" to the bottom of the TableView. By this I mean that the footerview will always be shown (which is not something I want)
I finally used self.tableView.footerView = MyView. I disabled ALL the spring and struct settings on my UIView and it fits perfectly. (I've previously set it to maximize hoping to have it maximize when there is nothing in the table)
Regarding centering the activity indicator: I ended up setting the footer view to hidden if there's nothing in the UITableView (by checking in numberOfRows). I programmatically added the ActivityIndicator in the center if this occurs.
Thanks for all the answers!!
Did you try to do it in the proper delegate for UITableview Footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Use This code to add any view in the footer.
e.g.
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.tblView.frame.size.width, 49)];
return footerView;
}

iPhone - changing contents of a UITableViewCell inside a navigationController

I have a UITableView with some cells in it that have people's names. When the user clicks on a cell, the Navigation Controller pushes a new view onto the stack that shows detailed info for that person. When the user is done and clicks the button to return to the previous view, the tableview is again shown. The problem is that I want to edit the cell text (or just mark the cell in some way) to show that the user has clicked on that cell. In my cellForRowAtIndexPath: method I have set up code that should change the text of the cell after a user clicks on that cell. I set up break points and it IS entering cellForRowAtIndexPath: when the detail view is popped and the tableview is again shown. It's entering the segment of code in cellForRowAtIndexPath: that should change the text, but when it comes up on screen, the text has not been changed. Does anyone have any ideas as to what is going on? Thanks.
EDIT: I figured it out. I just needed to call [tableView reloadData]; instead of calling the cellForRowAtIndexPath method. Not sure why that works differently, but it fixed it.
I guess I'll help you out. You don't need to credit me with the answer though.
cellForRowAtIndexPath:
that method is called within the framework when a cell is being refreshed from the queue or when it needs to be created. You should not call this method.
[tableView reloadData];
that method is basically a refresh on all of the visible cells show in the UITableView. This is the method you should call if you change information in your data source or you need to (force) update the appearance of your cells.
It's Good You Have Reload Table And Changed The Text But If YouJustIndicate NAd Don't Want To Change The Text The nYou Can Use elow Given Text ....
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
It Will Check Mark Particuler Cell Which You Click/Visited......
Hope You Like This Solution.....

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

IPhone custom UITableViewCell Reloading

currently I'm struggling with this problem:
I got a UITableViewController that displays a tableView with different custom cells.
One custom cell displays a number (by a label). If you click on this cell, the navigationController moves to a UIPicker where the user can select the number to be displayes.
If the user moves back, the cell should display the updated value.
Problem:
I managed to reload the cells by calling
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
in the UITableViewController.
This works only, if I don't use dequeueReusableCellWithIdentifier for the cell (tables won't show updates otherwise).
But in this case, memory usage grows and grows...
In addition, the program crashes after about 15 movements to pickerView and back - I think because the cell that should be reloaded is already released.
How can I update a reusable custom cell every time the view appears ?
What is the best solution ?
I think retaining cells should not be used ?
A general approach is to avoid reloading the whole table if only one cell is changed.
In such case, just get the reference to the UITableViewCell you want to "refresh" and invoke a [self setNeedsDisplay] from the main thread on it to trigger its refresh (will call the drawRect on it to trigger its drawing).
Have you set the appropriate id in the NIB? It has to be the same as you use when calling dequeueReusableCellWithIdentifier:
See step 3 here

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.