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.
Related
In my UITableview i have option for user to delete the row, i have done that but here i need to add a EDIT button with the DELETE button like the picture,
also when user clicks edit i want to allow the user to edit the ROW TEXT.. is it possible..?
Please help.
First of all you should ask yourself if you really need that edit button.
The user told you already that he wants to edit the tableView by tapping the edit button of the tableView. In my opinion there is absolutely no need for another edit button, the user will assume that everything he does in edit mode will edit the data.
There are two options:
Put the editing in another view controller that you push when the user taps the cell in edit mode.
For this you have to set the editingAccessoryType of the cell to UITableViewCellAccessoryDisclosureIndicator, to indicate that the cell can be selected.
And allowsSelectionDuringEditing of the tableView has to be set to YES.
Everything else is like handling row selections when not editing. Simply check for [tableView isEditing] in tableView:didSelectRowAtIndexPath:.
Replace the UILabel of your cell with a UITextField with a borderStyle of UITextBorderStyleNone and the same font as the UILabel. Set enabled to NO. This way it will look exactly like a UILabel.
Implement setEditing:animated: of the UITableViewController to enable each textField in editing mode
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
for (UITableViewCell *cell in [self.tableView visibleCells]) {
UITextField *tf = ...
tf.enabled = editing;
}
}
when the user taps the UITextField in edit mode he can input text.
If you really need that edit button add it in - (void)setEditing:(BOOL)editing animated:(BOOL)animated when editing is YES and remove it when editing is NO. Instead of adding and removing it I would just use setHidden:
Yes it is possible. You'll have to use the UITextField to show the text.
When user click on the Edit button you'll have to save the tag of the button. Save this tag so that you know which row is clicked. And then reload the table. While reloading the table make sure you set the userInterActionEnabled property of the UITextField at the particular row to true. It'll allow user to edit the text.
Create Custom cell and add your UIButtons and UITextField and just implement some logic. just refer tutorial from below links.
For Custom cell just see these below links with tutorials and demos:
custom-table-view-cells-in-ipad-programming
creating-custom-table-view-cell
Custom UITableViewCell in IB
I want to know the delegate method that gets called when the user cancel the delete operation of the UITableViewCell. Now to answer the potential question that "Why do I need this?", following is the scenario:
I have a table view where the item is displayed (which is left aligned to the main view) in a UILabel and its price is displayed in a separate UILabel (which is right aligned to the main view).
Now once the user presses the red (kind of no entry) button to delete any item, the whole cell is indented to the left and half of the price is clipped because of being out of the view. This looks quite ugly and hence I hide the price label upon press of this 'pre-delete' button (which works fine). But I want to display the price tag back when the user dismiss the delete button without deleting the cell. but I am unable to find the cancelDelete kind of event for tableview cell.
Thanx :-)
Here's something that might work instead, it stops the tableview from indenting the cells while editing:
Set UITableView's shouldIndentWhileEditing property to NO.
Implement the delegate-method tableView:editingStyleForRowAtIndexPath: method:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
Or you can do this when you create your cells:
cell.shouldIndentWhileEditing = NO;
Hopefully this works for you.
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.....
I am using an UIButton to show and toggle a table-cell-item from read to unread.
I used a blue image as a background to show when an item was unread, and that worked, but figured that it would be better to use the setHighlighted feature, as it saves adding an extra image to the App. The code I use is as follows:
- (void)updateReadButton{
if(article.read.boolValue){
//[readButton setBackgroundImage:nil forState:UIControlStateNormal];
[readButton setHighlighted:FALSE];
[readButton setNeedsLayout];
} else {
[readButton setHighlighted:TRUE];
[readButton setNeedsLayout];
}
}
This works fine for the initial creation of the cell. But when the item is clicked and the detail-view is shown I toggle the 'read' value to 'true' and change the setHighlighted option to false of the UIButton in the cell, but it doesn't change when coming back from the detail view. Only when the cell is scrolling off the screen and recreated is the change reflected. How can I force a redraw of the button once I navigate to the detail view?
You may be able to accomplish the highlight by simply reloading the cell in question. Peek at UITableView's - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation and use it to reload the cell after setting the highlight.
I don't think the method setHighlighted: is the correct one. Here is the documentation:
Specify YES if the control is
highlighted; otherwise NO.
By default, a control is not
highlighted. UIControl automatically
sets and clears this state
automatically when a touch enters and
exits during tracking and when there
is a touch up.
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"