UITableView with custom cells not in edit mode

I have a UITableView with custom UITableViewCells.

  • The table has two sections: the first section has one line with a UITextField and can only be edited in terms of text. This section and row cannot be edited in terms of a UITableView

  • The second section is a list of cells that are generated from NSArray. These cells are again regular UITableViewCells, consisting of two UITextFields. These cells can be edited in terms of a UITableView in the sense that the user can delete and insert rows.

  • In my designated initializer, I specified self.tableView.editing = YES , also I implemented the canEditRowAtIndexPath method to return YES.

Problem Statement

The table view is not in edit mode. I do not see the delete button or the insert button in the lines of section 2. What am I missing?

+6
source share
2 answers

just a suggestion, check if your controller meets these requirements:

I am using a regular UIViewController and it works fine - you need:

  • make your controller a delegate of UITableViewDelegate, UITableViewDataSource
  • implementation - (void) setEditing: (BOOL) editing animated: (BOOL) animated
  • programmatically add an EDIT button - self.navigationItem.rightBarButtonItem = self.editButtonItem (if you add an EDIT button from the builder, you will need to call setEditing: YES manually)

Part of the 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!

+6
source

What if you [self tableView setEditing:YES animated:YES]; instead of self.tableView.editing = YES; ?

+1
source

Source: https://habr.com/ru/post/890650/


All Articles