设置TableView setEditing时无法选择UITableViewCell

Ada*_*hns 5 iphone objective-c uitableview ios

我希望能够选择多行,如下面显示的默认邮件应用程序:

在此输入图像描述

我有一个叫做编辑的按钮

[self.myTableView setEditing:YES animated:YES]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

#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];
    }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*lum 15

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

[self.tableView setAllowsSelectionDuringEditing:YES];
Run Code Online (Sandbox Code Playgroud)

要么

[self.tableView setAllowsMultipleSelectionDuringEditing:YES];
Run Code Online (Sandbox Code Playgroud)

根据文档:这些属性NO默认设置为.

如果此属性的值为YES,则用户可以在编辑期间选择行.默认值为NO.如果要在不考虑模式的情况下限制单元格的选择,请使用allowSelection.

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Run Code Online (Sandbox Code Playgroud)