Changing the editing control of a UITableViewCell
I overrode the method:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
to make a plus button appear next to a cell. After the user edits the cell's contents, I want to change it from a plus button to a minus button. The logic is already written in this method. The problem is that even though this method is called and it's returning the correct value it doesn't update the button.
Using:
[myViewCell setNeedsLayout];
... doesn't cause [myTableView tableView:editngStyleForRowatIndexPath:]
to be run.
Calling
[tableView reloadRowsAtIndexPaths: ... withRowAnimation:UITableViewRowAnimationNone];
... does something weird... it causes [myTableView tableView:editngStyleForRowatIndexPath:]
to be called on all rows EXCEPT the one I send in the reloadRowsAtIndexPaths
parameter.
Regardless, since I am inserting a row, the method is being called on all my rows, including the one I want to 开发者_运维问答change. I checked that it is returning the UITableViewCellEditingStyleDelete
. Even though it returns the correct value, though, it still shows the plus icon instead of the minus icon.
Calling [tableview reloaddata]
does not work.
There is an editingStyle
property on the UITableViewCell
, which seems like it would solve my problems, but it is read only.
It is as if once the tableView:editingStyleForRowAtIndexPath:
method has set the editingStyle
, this can never be changed. Why, then, would this method be continuously called when rows are scrolled into view?
How can I change the editing control of a UITableViewCell?
You'll notice that in Apple's Contacts application, there is the same exact behavior, except that it is transitioning from UITableViewCellEditingStyleNone
instead of UITableViewCellEditingStyleInsert
. This can be seen by editing a contact, and then typing a number in the last phone field (i.e. adding a new phone number): the current row gets a minus button, and a new row is added beneath it.
I had similar problems two places in my app. I couldn't find a way to change or update the editingStyle of cells while editing and it was starting to drive me crazy.
I had one cell where I wanted to add a minus when the user entered something, and another where I wanted to remove a plus when the user clicked the cell and transformed it into a valid field.
I eventually solved both problems by calling
- (void) prepareForReuse;
on the cell in question. I don't think it's an elegant solution, but it somehow works. If I call this method before I start editing it causes some strange animation glitches, but if I call it after updating the underlying datastructures and within tableView beginUpdates
and tableView endUpdates
it animates perfectly for the case where I want to add the minus sign.
In my second case the plus sign is removed without animation by calling this method just before an empty tableView beginUpdates
and tableView endUpdates
sequence. It looks a bit abrupt, but I can live with that as long as the functionality is there.
精彩评论