设置了TableView setEditing后,无法选择UITableViewCell [英] Can't select UITableViewCell when TableView setEditing is set

查看:46
本文介绍了设置了TableView setEditing后,无法选择UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to select multiple rows like the default mail app shown below:

我有一个名为修改"的按钮,该按钮会调用

I have a button called edit that calls

[self.myTableView setEditing:YES animated:YES]

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

The edit button successfully shows the circles on the left of the cells like the mail app as shown above. However, when I actually select one of the rows, nothing happens. The red checkmark does not appear in the circle as I would expect. Why isn't the red checkmark appearing?

#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];
    }
}

推荐答案

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

You have to explicitly set selection to be enabled during editing mode:

[self.tableView setAllowsSelectionDuringEditing:YES];

[self.tableView setAllowsMultipleSelectionDuringEditing:YES];

根据

According to the docs: these properties are set to NO by default.

如果此属性的值为YES,则用户可以在以下过程中选择行编辑.默认值为否".如果您想限制选择单元格(无论模式如何),请使用allowSelection.

If the value of this property is YES , users can select rows during editing. The default value is NO. If you want to restrict selection of cells regardless of mode, use allowsSelection.

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

Additionally, the following snippet of code may be causing problems with your selection because it immediately deselects the row as soon as it's been selected.

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

这篇关于设置了TableView setEditing后,无法选择UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆