在编辑模式下选择UITableViewCell不会';t在左侧显示圆形复选标记

在编辑模式下选择UITableViewCell不会';t在左侧显示圆形复选标记,uitableview,editing,multipleselection,Uitableview,Editing,Multipleselection,我有一个UITableViewController,当用户按下工具栏上的按钮时,它会切换到编辑模式。我希望用户选择多个单元格,然后在每个选定单元格的左侧打上一个圆形的红色复选标记。我在序列图像板的表格视图中编辑时选择了多个选项,并且没有自定义单元格的附件/编辑附件 问题是,我可以在tableView的indexPathsForSelectedRows中找到每个点击的单元格,但每个选定单元格左侧的红色复选标记不会出现。但是,在离开编辑模式后,每个选定的单元格都会在右侧显示一个复选标记附件(编辑完成

我有一个UITableViewController,当用户按下工具栏上的按钮时,它会切换到编辑模式。我希望用户选择多个单元格,然后在每个选定单元格的左侧打上一个圆形的红色复选标记。我在序列图像板的表格视图中编辑时选择了多个选项,并且没有自定义单元格的附件/编辑附件

问题是,我可以在tableView的indexPathsForSelectedRows中找到每个点击的单元格,但每个选定单元格左侧的红色复选标记不会出现。但是,在离开编辑模式后,每个选定的单元格都会在右侧显示一个复选标记附件(编辑完成后我不再需要它)

编辑时: 编辑后:

以下是我在代码中所做的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView.editing)
   {
       UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
       if ([selectedCell accessoryType] == UITableViewCellAccessoryNone)
       {
           [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
       }
       else
       {
           [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
       }
   }
}


谢谢

如果有人遇到同样的问题,这里是我如何解决它的剪报。我有一个连接到iAction的工具栏按钮,可以切换UITableView的编辑模式。启用编辑后,用户可以选择行并点击删除按钮,该按钮在其标签中设置了所选行数

@interface TableViewController ()
{
    NSMutableArray      *selectedCellRows;      // Used to remember which cells have been selected during editing mode
}

- (IBAction)toggleEditing:(id)sender;

@end


@implementation TableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GeneralReservationCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];

    // Set up the cell...
    [self configureCell:cell forTableView:tableView atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(GeneralReservationCell *)cell forTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
    // Basic layout
    cell.nameLabel.text = @"Some text";

    cell.selected = NO;
    if (tableView.editing)
    {
        for (NSIndexPath *selectedIndex in selectedCellRows)
        {
            if ([selectedIndex isEqual:indexPath])
            {
                cell.selected = YES;
                break;
            }
        }
    }
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView.editing)
    {
        [selectedCellRows addObject:indexPath];
        [self updateEditingToolbarButton];
    }
    else
    {
        // Do whatever you do normally when the cell gets selected
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView.editing)
    {
        [selectedCellRows removeObject:indexPath];
        [self updateEditingToolbarButton];
    }
}


- (void)updateEditingToolbarButton
{
    int selectedRows = [self.tableView.indexPathsForSelectedRows count];

    if (selectedRows == 0)
    {
        [deleteBarButton setTitle:@"delete"];
        deleteBarButton.enabled = NO;
    }
    else
    {
        [deleteBarButton setTitle:[NSString stringWithFormat:@"delete (%d)", selectedRows]];
        deleteBarButton.enabled = YES;
    }
}


#pragma mark - IBActions

- (IBAction)toggleEditing:(id)sender
{
    if(self.tableView.editing)
    {
        [self.tableView setEditing:NO animated:YES];
        [selectedCellRows removeAllObjects];
        [self showToolbarInEditingMode:NO];
    }
    else
    {
        [self.tableView setEditing:YES animated:YES];
        [self showToolbarInEditingMode:YES];
    }
}


@end

此外,我在Interface Builder中将编辑附件设置为
none

如果有人遇到同样的问题,下面是我如何解决它的剪报。我有一个连接到iAction的工具栏按钮,可以切换UITableView的编辑模式。启用编辑后,用户可以选择行并点击删除按钮,该按钮在其标签中设置了所选行数

@interface TableViewController ()
{
    NSMutableArray      *selectedCellRows;      // Used to remember which cells have been selected during editing mode
}

- (IBAction)toggleEditing:(id)sender;

@end


@implementation TableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GeneralReservationCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];

    // Set up the cell...
    [self configureCell:cell forTableView:tableView atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(GeneralReservationCell *)cell forTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
    // Basic layout
    cell.nameLabel.text = @"Some text";

    cell.selected = NO;
    if (tableView.editing)
    {
        for (NSIndexPath *selectedIndex in selectedCellRows)
        {
            if ([selectedIndex isEqual:indexPath])
            {
                cell.selected = YES;
                break;
            }
        }
    }
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView.editing)
    {
        [selectedCellRows addObject:indexPath];
        [self updateEditingToolbarButton];
    }
    else
    {
        // Do whatever you do normally when the cell gets selected
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView.editing)
    {
        [selectedCellRows removeObject:indexPath];
        [self updateEditingToolbarButton];
    }
}


- (void)updateEditingToolbarButton
{
    int selectedRows = [self.tableView.indexPathsForSelectedRows count];

    if (selectedRows == 0)
    {
        [deleteBarButton setTitle:@"delete"];
        deleteBarButton.enabled = NO;
    }
    else
    {
        [deleteBarButton setTitle:[NSString stringWithFormat:@"delete (%d)", selectedRows]];
        deleteBarButton.enabled = YES;
    }
}


#pragma mark - IBActions

- (IBAction)toggleEditing:(id)sender
{
    if(self.tableView.editing)
    {
        [self.tableView setEditing:NO animated:YES];
        [selectedCellRows removeAllObjects];
        [self showToolbarInEditingMode:NO];
    }
    else
    {
        [self.tableView setEditing:YES animated:YES];
        [self showToolbarInEditingMode:YES];
    }
}


@end

此外,我在Interface Builder中将编辑附件设置为“无”。

如果希望在选中单元格时填充圆形,则需要将单元格的selectionStyle设置为“无”以外的其他选项

cell.selectionStyle = .blue
这也可以在界面生成器中设置

另外,如果不编辑tableView时不希望选中该复选标记,则需要在
else
块中将单元格的accessoryType设置为
.none

if tableView.isEditing {
  ...
} else {
 cell.accessoryType = .none
}

如果要在选中单元格时填充圆形,则需要将单元格的selectionStyle设置为非none

cell.selectionStyle = .blue
这也可以在界面生成器中设置

另外,如果不编辑tableView时不希望选中该复选标记,则需要在
else
块中将单元格的accessoryType设置为
.none

if tableView.isEditing {
  ...
} else {
 cell.accessoryType = .none
}

你找到解决办法了吗?我面临同样的问题,我试图以multipleEditMode访问左侧的属性,但找不到正确的属性。请查看下面的答案。您找到解决方案了吗?我面临同样的问题,我正在尝试以multipleEditMode访问左侧的属性,但找不到正确的属性。请查看下面我的答案。感谢您的回答,我已经用另一种方式解决了此问题。出于兴趣,无法访问左侧的复选标记属性?看起来你用另一种方式处理了不,我用的是标准的复选标记。如果您想访问复选标记的视图,以便用自己的替换它,可以使用
cell.accessoryView
。感谢您的回答,我已经用另一种方式解决了这个问题。出于兴趣,无法访问左侧的复选标记属性?看起来你用另一种方式处理了不,我用的是标准的复选标记。如果要访问复选标记的视图,以便用自己的替换它,可以使用
cell.accessoryView