简体   繁体   English

UITableView,自定义单元格未进入编辑模式

[英]UITableView with custom cells not entering editing mode

I have a UITableView with custom UITableViewCells. 我有一个UITableView与自定义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. 该表有两个部分,第一部分有一行带有UITextField,只能根据文本进行编辑。 This section & row cannot be edited from a UITableView perspective 无法从UITableView透视图编辑此部分和行

  2. The second section is a list of cells that are generated from an NSArray. 第二部分是从NSArray生成的单元格列表。 These cells are once again custom UITableViewCells comprising of two UITextFields. 这些单元格再次是由两个UITextField组成的自定义UITableViewCells。 These cells can be edited from a UITableView perspective, in the sense that the user can delete and insert rows. 从用户可以删除和插入行的意义上来说,可以从UITableView透视图编辑这些单元格。

  3. In my designated initializer I have specified self.tableView.editing = YES , also I have implemented the method canEditRowAtIndexPath to return YES. 在我指定的初始化程序中,我指定了self.tableView.editing = YES ,我也实现了方法canEditRowAtIndexPath以返回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? 我没有看到删除按钮或插入第2节行的按钮。我缺少什么?

just a suggestion, check whether your controller fit to these requirements : 只是一个建议,检查您的控制器是否符合这些要求:

i use usual UIViewController and it works fine - you need to : 我使用通常的UIViewController,它工作正常 - 你需要:

  1. make your controller a delegate of UITableViewDelegate, UITableViewDataSource 使您的控制器成为UITableViewDelegate,UITableViewDataSource的委托
  2. implement - (void)setEditing:(BOOL)editing animated:(BOOL)animated 实现 - (void)setEditing:(BOOL)编辑动画:(BOOL)动画
  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) 以编程方式添加EDIT按钮 - self.navigationItem.rightBarButtonItem = self.editButtonItem(如果您从构建器添加EDIT按钮,则需要手动调用setEditing:YES)

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]; 如果你这样做[self tableView setEditing:YES animated:YES]; instead of self.tableView.editing = YES; 而不是self.tableView.editing = YES; ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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