如何以编程方式selectuitableview行

我试图实现一个电子邮件,如selectuitableview中的所有function,在相同的button水龙头用户可以勾选或删除所有复选标记,另外用户还可以select/取消select行(在didSelectRowAtIndexPath)。 我试图做但不能正常工作,这是我的代码。

- (IBAction)selectAll:(id)sender { if(myBoolean) { for (NSInteger s = 0; s < self.iTable.numberOfSections; s++) { for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++) { [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone]; } } myBoolean = NO; [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal]; } else { for (NSInteger s = 0; s < self.iTable.numberOfSections; s++) { for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++) { [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark]; NSLog(@"%d-%d",s,r); } } myBoolean = YES; [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal]; } } -(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { cell.accessoryType = UITableViewCellAccessoryNone; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; } } 

设置附件视图需要在tableView:cellForRowAtIndexPath:方法发生。 当你想从外部改变附件的时候,外面的方法需要先改变模型,以表明复选标记必须放在某些单元格中,然后在UITableView上调用reloadData

存储检查单元格的一种方法是一个NSIndexSet对象数组 – 每个部分一个索引集。 在下面的示例中,我展示了单个部分的代码,但是您应该了解如何使多个部分工作。

 // This variable needs to be declared in a place where your data source can get it NSMutableIndexSet *selected; // You need to initialize it in the designated initializer, like this: selected = [[NSMutableIndexSet alloc] init]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } if ([selected containsIndex:indexPath.row]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } else { [cell setAccessoryType:UITableViewCellAccessoryNone]; } // Do the rest of your code return cell; } 

现在在你想要设置行select或取消select的代码中,只需调用[selected addIndex:rowToSelect][selected removeIndex:rowToUnselect] ,然后调用你的表的reloadData

我写了一个适合您的需求的示例代码 。

基本上是

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *unifiedID = @"aCellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID]; } cell.textLabel.text = [self.states objectAtIndex:indexPath.row]; //if the indexPath was found among the selected ones, set the checkmark on the cell cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *state = [self.states objectAtIndex:indexPath.row]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){ [self.selectedCells removeObject:indexPath]; [self.selecedStates removeObject:state]; cell.accessoryType = UITableViewCellAccessoryNone; } else { [self.selectedCells addObject:indexPath]; [self.selecedStates addObject:state]; cell.accessoryType = UITableViewCellAccessoryCheckmark; } NSLog(@"%@", self.selecedStates); } -(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath { return ([self.selectedCells containsObject:indexPath]) ? YES : NO; } - (IBAction)selectAll:(id)sender { [self.selecedStates removeAllObjects]; [self.selectedCells removeAllObjects]; NSUInteger numberOfSections = [self.tableView numberOfSections]; for (NSUInteger s = 0; s < numberOfSections; ++s) { NSUInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:s]; for (NSUInteger r = 0; r < numberOfRowsInSection; ++r) { NSIndexPath *idxPath = [NSIndexPath indexPathForRow:r inSection:s]; [self.selectedCells addObject:idxPath]; [self.selecedStates addObject:self.states[idxPath.row]]; } } [self.tableView reloadData]; } - (IBAction)deselectAll:(id)sender { [self.selecedStates removeAllObjects]; [self.selectedCells removeAllObjects]; [self.tableView reloadData]; } - (IBAction)toggleAll:(id)sender { if ([self.states count] == [self.selecedStates count]) { [sender setTitle:@"select all"]; [self deselectAll:sender]; } else { [sender setTitle:@"deselect all"]; [self selectAll:sender]; } } 

在行动:

在这里输入图像说明

你在打电话

 [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone]; 

为tableView中的每一节中的每一行。 如果你有很多行,这是非常低效的,因为它将处理不在屏幕上的行。 但是这不是必需的。 只需将每个选定的索引path放入一个数组中,并告诉tableView重新加载。 这将重新加载可见的单元格,并且由于新的行的-tableView:cellForRowAtIndexPath:单元格的实现将被正确地重新configuration。

使用selectRowAtIndexPath:animated:scrollPosition:select一行
deselectRowAtIndexPath:animated:取消select一行。
更多阅读UITableView文档

试试这个代码,而不是你的旧的

 - (IBAction)selectAll:(id)sender { if(myBoolean) { for (NSInteger s = 0; s < self.iTable.numberOfSections; s++) { for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++) { [self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone]; } } myBoolean = NO; [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal]; } else { for (NSInteger s = 0; s < self.iTable.numberOfSections; s++) { for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++) { [self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone]; NSLog(@"%d-%d",s,r); } } myBoolean = YES; [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal]; } } -(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { cell.accessoryType = UITableViewCellAccessoryNone; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; } } 

你可以按照这些步骤来实现这个,

1)你应该有一个mutable数组来存储索引path

2)你可以做的是,当你点击全部选中或取消全部选中时, addremove所有的数组(或你已经)的索引path,重新加载表更新更改

3)在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath数据源方法,使用if([array containsObject:indexPath])检查数组,如果存在标记它checked或未unchecked

4)在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath委托方法,你应该检查已经存在的数据库中的索引path,如第3步中所做的, 添加删除 indexpaths根据条件,重新加载表更新更改

把另一个NSMutableArray作为SelectedArray

didSelectRowAtIndexPath行中您可以从SelectedArray中添加删除对象。

你可以select一个UITableViewCell调用UITableViews selectRowAtIndexPath方法:

 [yourtable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop]; 

selectedArray的循环放在选中的单元格或所有单元格中。

在这里检查我接受的答案: 需要为UITableView创build一个全选button,并将select添加到iOS中的数组

  • Xcode 8x
  • Swift 3x
  • select行

     let indexPath = IndexPath(row: 0, section: 0) mytableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom) myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath) 

    DeSelect Row

     let deselectIndexPath = IndexPath(row: 7, section: 0) myTableView.deselectRow(at: deselectIndexPath, animated: true) myTableView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath) 

    你可以做这样的事情:

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self doSomethingWithRowAtIndexPath:indexPath]; }