问题
I have a UITableView with custom UITableViewCells.
The table has two sections, the first section has a single row with a UITextField and can only be edited in terms of the text. This section & row cannot be edited from a UITableView perspective
The second section is a list of cells that are generated from an NSArray. These cells are once again custom UITableViewCells comprising of two UITextFields. These cells can be edited from a UITableView perspective, in the sense that the user can delete and insert rows.
In my designated initializer I have specified
self.tableView.editing = YES
, also I have implemented the methodcanEditRowAtIndexPath
to return YES.
Problem Statement
The table view does not enter editing mode. I do not see the delete buttons or insert buttons against the rows of section 2. What am I missing?
回答1:
just a suggestion, check whether your controller fit to these requirements :
i use usual UIViewController and it works fine - you need to :
- make your controller a delegate of UITableViewDelegate, UITableViewDataSource
- implement - (void)setEditing:(BOOL)editing animated:(BOOL)animated
- programmatically add EDIT button - self.navigationItem.rightBarButtonItem = self.editButtonItem (if you add EDIT button from builder you will need to call setEditing : YES manually)
Piece of code :)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
- (void)tableView
:(UITableView *)tableView didSelectRowAtIndexPath
:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// do not forget interface in header file
@interface ContactsController : ViewController<
UITableViewDelegate,
UITableViewDataSource>
Profit!
回答2:
What if you do [self tableView setEditing:YES animated:YES];
instead of self.tableView.editing = YES;
?
来源:https://stackoverflow.com/questions/6367007/uitableview-with-custom-cells-not-entering-editing-mode