本文介绍了带有自定义单元格的 UITableView 未进入编辑模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 UITableViewCells 的 UITableView.

I have a UITableView with custom UITableViewCells.

  1. 表格有两部分,第一部分有一个带有 UITextField 的单行,并且只能在文本方面进行编辑.本节&无法从 UITableView 角度编辑行

  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

第二部分是从 NSArray 生成的单元格列表.这些单元格再次是由两个 UITextField 组成的自定义 UITableViewCells.这些单元格可以从 UITableView 的角度进行编辑,因为用户可以删除和插入行.

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.

在我指定的初始值设定项中,我指定了 self.tableView.editing = YES,并且我还实现了方法 canEditRowAtIndexPath 以返回 YES.

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

问题陈述

表格视图没有进入编辑模式.我在第 2 部分的行中没有看到删除按钮或插入按钮.我错过了什么?

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 :

我使用通常的 UIViewController 并且它工作正常 - 你需要:

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

  1. 让你的控制器成为 UITableViewDelegate、UITableViewDataSource 的委托
  2. 实现 - (void)setEditing:(BOOL)editing animation:(BOOL)animated
  3. 以编程方式添加编辑按钮 - self.navigationItem.rightBarButtonItem = self.editButtonItem(如果您从构建器添加编辑按钮,则需要手动调用 setEditing : YES)

一段代码:)

- (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>

利润!

这篇关于带有自定义单元格的 UITableView 未进入编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:54
查看更多