I'm trying to mimic the behavior of the editable table cells in the contacts app. I've subclassed UITableViewCell, added my own UITextField, and setup behavior where if edit is pressed, the label gets hidden and my text开发者_开发知识库field becomes enabled, and shows, and vice versa. That part works fine.
I don't want my users to be able to delete rows, but I'm having trouble finding where/how to disable and hide the delete button on the left of a cell itself. Any advice would be appreciated.
I should have been more clear. I want the edit button to work to hide and show various things in the cell, but I don't want the delete button to show.
I found that doing the following for the cells I don't want deleted works:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
The view still slides to the right when edit is pressed, and my cell goes into edit mode, but the delete button is gone.
Chuck you cannot override the method called when you click on the (-) button.But you can detect by checking the currrent mode of table whether its in editing mode or not. So what you have to do is to keep your own custon (-) button and on the click on which you can set whatever you want to do and it wont display delete button also.. Exactly what you want :D
hAPPY iCODING...
Not quite the same, but what I wanted was, if a cell can be deleted, to show the delete button, and if not, pop up an alert to explain why it can't to the user.
I found this tricky because whilst I could disable the delete button when a cell isn't to be deleted, it also meant that a swipe-right would not call any action that I could use to trigger the alert.
So what I do, is allow all cells to be "deleted" by returning YES from tableView:canEditRowAtIndexPath
, and then use the following code to:
Popup the alert, and
Cause the delete button to be removed before the user can tap it.
// This will be called when the user dismisses the alert. // (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [self.tableView setEditing:NO animated:YES]; } (void) tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { Item *item = [self itemForIndexPath:indexPath]; if ([item canNotBeDeleted] == YES) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Item in use." message:@"Sorry but you can't delete a grocery item that has been included in a shopping list." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } }
精彩评论