iPhone didSelectRowAtIndexPath not firing - iphone

So I am trying to default to having the first row in a tableView selected when my view first loads. I am doing this my viewWillAppear method:
NSIndexPath *tempPath= [NSIndexPath indexPathForRow:0 inSection:0];
[theTable.delegate tableView:theTable didSelectRowAtIndexPath:tempPath];
Which fires the method, but the cell doesn't stay selected. This fires the proper method and executes the code in there, and I am never deselecting it. After the user presses another cell, it stays selected properly. Just not the first time. Does anyone know what may be causing this weirdness? I have even tried to do this:
[[theTable.delegate tableView:theTable cellForRowAtIndexPath:tempPath] setSelected:YES];
Thoughts?

First of all, you shouldn't be calling the delegate methods directly like that.
If you do need to select a cell programmatically (like the Mail app does), use the selectRowAtIndexPath method:
[theTable selectRowAtIndexPath:tempPath animated:YES scrollPosition:UITableViewScrollPositionBottom];

According to the HIG, you aren't supposed to leave table view cells in a selected state. You are encouraged (strongly) to deselect the row as soon as you perform whatever action the tap requires.
For persistent selections, you are supposed to use the accessory, and you can set a cell's accessory to a checkmark to indicate "Selected"

Related

UITableView deselect selected cells after editing

I have multiple cells selected in a UITableView, but they automatically get deselected after editing. I searched a bit and I saw people saying that they called 'reloadData' which has caused the selections to be removed, but I do not call reloadData anywhere except for in 'viewDidAppear'. They get deselected even if the editing has been cancelled without changing anything (when clicking 'edit' button in the toolbar then clicking 'done' button without any editing). I'm not sure where this is happening. Or, if I could detect when the editing has been finished (when 'done' button has been clicked), I could simply reselect all the cells that had been selected, but I'm not sure how I could detect it..
How could I prevent the cells from getting deselected? Or reselect the cells after the edit?
Thanks in advance.
I presume you are trying to achieve selection and deselection on Button press, If you are trying to change the state of selection in a UITableViewCell try using the method in the button action.
-(IBAction) someAction {
if(buttonToggled){
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}else{
//Do something
}
}
for changing the backgroundColor.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];
for more information on the above please check the link.
UITableView Cell selected Color?
I would suggest the best bet is just to reselect them yourself after every press on your "done" button.
It is quite normal that cells get deselected or buttons get unpressed, since this is their normal state - exept if you write your own code to change this - like in your case.
This is how to select a row:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
If you have multiple rows that can be selected make sure you have the settings accordingly, otherwise it might always deselect all but the most recent rows.
Nevermind. I found out that - (void)setEditing:(BOOL)editing animated:(BOOL)animated gets called whenever the 'edit' or the 'done' button has been clicked.

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

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

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.