【问题标题】:Can't select UITableViewCell when TableView setEditing is set设置 TableView setEditing 时无法选择 UITableViewCell
【发布时间】:2013-09-03 20:59:02
【问题描述】:

我希望能够像下面显示的默认邮件应用一样选择多行:

我有一个名为编辑的按钮

[self.myTableView setEditing:YES animated:YES]

编辑按钮成功显示单元格左侧的圆圈,如上图所示的邮件应用程序。但是,当我实际选择其中一行时,什么也没有发生。正如我所料,红色复选标记没有出现在圆圈中。为什么没有出现红色复选标记?

#pragma mark - UITableViewDataSource Methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
    }

    cell.textLabel.text = @"hey";

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 3;
}

#pragma mark - Private Methods

- (IBAction)editButtonTapped:(id)sender {
    if (self.myTableView.editing) {
        [self.myTableView setEditing:NO animated:YES];
    }
    else {
        [self.myTableView setEditing:YES animated:YES];
    }
}

【问题讨论】:

    标签: iphone ios objective-c uitableview


    【解决方案1】:

    您必须明确设置在编辑模式下启用选择:

    [self.tableView setAllowsSelectionDuringEditing:YES];
    

    或者

    [self.tableView setAllowsMultipleSelectionDuringEditing:YES];
    

    根据docs:这些属性默认设置为NO

    如果此属性的值为 YES ,用户可以在期间选择行 编辑。默认值为 NO。如果你想限制选择 单元格不管模式,使用allowSelection。

    此外,以下 sn-p 代码可能会导致您的选择出现问题,因为它会在选中该行后立即取消选择该行。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    【讨论】:

    • 谢谢!就是这样。也可以在表视图(不是表视图控制器)上的 IB 中更改为“编辑:编辑期间的单选”
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多