UITableView with custom cells not entering editing mode

隐身守侯 提交于 2020-01-14 08:55:09

问题


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?


回答1:


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!




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!