Editable UITableViewCell - EditingStyleForRow() not invoked
I am trying to implement deletable UITableViewCells. My UITableView has 3 sections. The 1st section contains one cell for every element of a List (called items for simplicity). Various elements (UILabels and a UIButton) are added to each cell.
Like most articles on the web, my code implements the delegate methods that are in charge of making cells editable:
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath) {
if (indexPath.Section == 0 && items.Count > 0) {
return true;
}
return false;
}
public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath) {
if (indexPath.Section == 0 && items.Count > 0) {
return UITableViewCellEditingStyle.Delete;
}
return UITableViewCellEditingStyle.None;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
items.RemoveAt(indexPath.Row);
tableView.DeleteRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.None);
}
}
Whether or not a cell is editable/deletable is decided in GetCell():
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
string cellIdentifier;
UITableViewCell cell;
if (indexPath.Section == 0) {
if (items.Count == 0) {
开发者_如何学编程 // Generate a "No data here..." cell
...
} else {
cellIdentifier = "ItemCell";
cell = tableView.DequeueReusableCell(cellIdentifier);
if (cell == null) {
// Create a new cell
...
}
cell.Editing = true; // Make the cell deletable
cell.ShouldIndentWhileEditing = true;
cell.ShowingDeleteConfirmation = true;
// Assign item values to the cell
...
}
} else if (...) { // Other sections go here
}
return cell;
}
Placing Console.Writes and breakpoints all over the place made me believe that the EditingStyleForRow() delegate method is not even invoked. None of it's Console.Writes showed up, no breakpoint in there was ever reached.
All the other delegates work fine though. Table contents show up as expected, just not editable/deletable.
Any hints on what I am doing wrong? Thanks in advance.
EDIT:
Apple docs say that UITableViewCellEditingStyle.Delete will be set for editable cells if the delegate method is not implemented. But it doesn't work without the method either.
EDIT 2:
Thanks to Ratinhos hint, EditingStyleForRow() is now invoked and correctly returns UITableViewCellEditingStyle.Delete, but the delete controls still don't appear.
did you invoke:
[tableView setEditing:animated:]
?
It works now. I completely removed those:
cell.Editing = true; // Make the cell deletable
cell.ShouldIndentWhileEditing = true;
cell.ShowingDeleteConfirmation = true;
Also, CanEditRow() doesn't really seem to be required. All that is left is tableView.Editing = true and the implementations of EditingStyleForRow() and CommitEditingStyle().
精彩评论