【问题标题】:Selecting UITableViewCell in editing mode doesn't show rounded checkmark on the left在编辑模式下选择 UITableViewCell 不会在左侧显示圆形复选标记
【发布时间】:2013-05-24 13:22:07
【问题描述】:

当用户按下工具栏中的按钮时,我有一个 UITableViewController 切换到其编辑模式。我希望用户选择多个单元格,然后在每个选定单元格的左侧放置一个圆形红色复选标记。我在 Storyboard 的表格视图中选择了 Multiple Selection during Editing,并且我的自定义单元格没有附件/编辑附件。

问题是我可以在 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];
       }
   }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  if (tableView.editing)
  {
      cell.accessoryType = UITableViewCellAccessoryNone;
      for (NSIndexPath *selectedIndex in [self.tableView indexPathsForSelectedRows])
      {
          if ([selectedIndex isEqual:indexPath])
          {
             cell.accessoryType = UITableViewCellAccessoryCheckmark;
             break;
          }
      }
  }
  ...

谢谢!

【问题讨论】:

  • 您找到解决方案了吗?我面临同样的问题,我试图在 multipleEditMode 中访问左侧的属性,但找不到正确的属性..
  • 请看下面我的回答。

标签: uitableview editing multipleselection


【解决方案1】:

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

@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 中将 Editing Accessory 设置为 none

【讨论】:

  • 感谢您的回答,我已经用另一种方式解决了这个问题。出于兴趣,有没有办法访问左侧的复选标记属性?看来你也用另一种方式处理它
  • 不,我使用标准复选标记。如果您想访问复选标记的视图以便将其替换为您自己的,您可以使用cell.accessoryView
【解决方案2】:

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

cell.selectionStyle = .blue

这也可以在界面生成器中设置。

另外,如果您在不编辑 tableView 时不想勾选,则需要在 else 块中将单元格的附件类型设置为 .none

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多