在单个节中循环遍历静态单元格.UITableView



我有一个静态UITableView,有许多节。其中一个包含许多单元格,这些单元格将成为选项(单击以勾选)。

我有一个NSMutableArray (self.checkedData),它包含所选行的行ID。我不知道如何循环遍历特定区域的单元格。检查行是否在数组中,如果在则添加一个复选标记。因此,当加载视图时,可以从coredata中提取选项,然后标记选中的行。

我目前有这个来处理添加复选标记。这很好。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the selected data from the IndexPath.row
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // determine the data from the IndexPath.row
    if ( ![self.checkedData containsObject:[NSNumber numberWithInt:indexPath.row]] )
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.checkedData addObject:[NSNumber numberWithInt:indexPath.row]];
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.checkedData removeObject:[NSNumber numberWithInt:indexPath.row]];
    }    
    [tableView reloadData];
}

您可以像这样获得特定部分中所有单元格的数组:

NSUInteger section = 0;
NSInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:section];
NSMutableArray *cellsInSection = [NSMutableArray arrayWithCapacity:numberOfRowsInSection];
for (NSInteger row = 0; row < numberOfRowsInSection; row++)
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cellsInSection addObject:cell];
}

cellsInSection数组现在包含了section 0

中的所有单元格

也许在viewDidLoad ?:

for(NSIndexPath *thisIndexPath in [self.tableView indexPathsForVisibleRows]) {
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  if ( ![self.checkedData containsObject:[NSNumber numberWithInt:indexPath.row]] ) {
      cell.accessoryType = UITableViewCellAccessoryCheckmark;
      [self.checkedData addObject:[NSNumber numberWithInt:indexPath.row]];
    } else {
      cell.accessoryType = UITableViewCellAccessoryNone;
      [self.checkedData removeObject:[NSNumber numberWithInt:indexPath.row]];
    }
   [self.tableView reloadData];
}

最新更新