简体   繁体   中英

UITableView with custom cells not entering editing mode

I have a UITableView with custom UITableViewCells.

  1. 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

  2. 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.

  3. In my designated initializer I have specified self.tableView.editing = YES , also I have implemented the method canEditRowAtIndexPath 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?

just a suggestion, check whether your controller fit to these requirements :

i use usual UIViewController and it works fine - you need to :

  1. make your controller a delegate of UITableViewDelegate, UITableViewDataSource
  2. implement - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  3. 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM