【问题标题】:UITableView with custom cells not entering editing mode带有自定义单元格的 UITableView 未进入编辑模式
【发布时间】:2011-09-16 01:22:24
【问题描述】:

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

  1. 该表有两个部分,第一部分有一个带有 UITextField 的单行,并且只能根据文本进行编辑。无法从 UITableView 角度编辑此部分和行

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

  3. 在我指定的初始化程序中,我指定了self.tableView.editing = YES,还实现了方法canEditRowAtIndexPath 以返回YES。

问题陈述

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

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    只是一个建议,请检查您的控制器是否符合这些要求:

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

    1. 让你的控制器成为 UITableViewDelegate、UITableViewDataSource 的代表
    2. 实现 - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    3. 以编程方式添加 EDIT 按钮 - self.navigationItem.rightBarButtonItem = self.editButtonItem(如果您从构建器添加 EDIT 按钮,则需要手动调用 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>
    

    利润!

    【讨论】:

    • 为什么要在 setEditing 中调用 super?这行不通,因为它是一个简单的 UIViewController。
    【解决方案2】:

    如果您使用[self tableView setEditing:YES animated:YES]; 而不是self.tableView.editing = YES; 会怎样?

    【讨论】:

    • 什么都不做 - 表格仍然没有显示针对每一行的编辑控件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    相关资源
    最近更新 更多