我有一个UITableViewController,当用户按下工具栏中的按钮时,它会切换到其编辑模式。我希望用户选择多个单元格,然后在每个选定单元格的左侧放置一个圆形红色复选标记。我在故事板的表格视图中选择了编辑期间的多个选择,而我的自定义单元格没有附件/编辑附件。
问题是我可以在tableView的 indexPathsForSelectedRows 中找到每个tapped单元格,但不会出现每个所选单元格左侧的红色复选标记。但是,在离开编辑模式后,每个选定的单元格在右侧显示一个复选标记附件(完成编辑后我不再需要它)。
编辑时:
编辑后:
以下是我在代码中所做的事情:
- (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;
}
}
}
...
谢谢!
答案 0 :(得分:1)
如果有人遇到同样的问题,这里有一个我已经解决过的问题。我有一个连接到IBAction的工具栏按钮,可以切换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
。
答案 1 :(得分:0)
如果要在选中单元格时填充圆角,则需要将单元格的selectionStyle设置为none。
useEffect(() => {
if (typeof window === 'undefined') return;
const handleResize = () => setWidth(window.innerWidth);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize)
};
});
这也可以在界面构建器中设置。
如果您在不编辑tableView时不希望选中标记,则需要在cell.selectionStyle = .blue
块中将单元格的annexType设置为.none
。
else