iphone - 如何获取TableView中同一组的所有行

标签 iphone objective-c uitableview

请帮我获取tableView中同一组的所有行。我正在使用下面的代码在表格 View 组中显示单选/检查,并希望每组仅选择一个。

所以我有两张图片 1:取消勾选 2:勾选 因此,当用户单击/选择一行组时,我将其 cell.image 更改为“已选中”及其工作状态,但不知道如何将同一组的所有其他单元格图像更改为“未选中”

不知道如何循环(获取)当前组的所有行/单元格

- (void)tableView:(UITableView *)tableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSArray *listData =[self.tableContents objectForKey:

                    [self.sortedKeys objectAtIndex:[indexPath section]]];

NSUInteger row = [indexPath row];

NSString *rowValue = [listData objectAtIndex:row];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.image = [UIImage imageNamed:@"checkbox-checked.png"];

NSString *message = [[NSString alloc] initWithFormat:rowValue];

UIAlertView *alert = [[UIAlertView alloc]

                      initWithTitle:@"You selected"

                      message:message delegate:nil

                      cancelButtonTitle:@"OK"

                      otherButtonTitles:nil];

[alert show];

[alert release];

[message release];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

最佳答案

这是一个可能的解决方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   // Store the section in your class
   NSUInteger section = indexPath.section;
   self.section = section; // remember to declare instance variable, property, synthesize
   self.row = row; // remember to declare instance variable, property, synthesize
   [tableView reloadData];
}

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   if (indexPath.section == self.section) {
      if (indexPath.row != self.row) {
         // then do UNCHECK here
      }
   }
}

如果你知道当前节有多少行,则不需要调用 reloadData,只需调用 [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:YOUR_ROW inSection:YOUR_SECTION];

关于iphone - 如何获取TableView中同一组的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3658265/

相关文章:

iphone - 这段上传图片的代码需要做哪些更改并从服务器获得响应

ios - 添加多个 subview 时 ScrollView 不滚动

iphone - 曲棍球与测试飞行

ios - 隐藏导航栏时,使用带有水平滚动的 CollectionView 的 interactivepopgesturerecognizer

ios - 显示屏左侧的 TouchesBegan 延迟

iOS Table View Cell Swift 4 中的滚动视差效果

iOS CustomTableViewCell : elements not aligining correctly

iphone - ios 存储自定义对象

iphone - UIPickerView 导致应用程序崩溃

php - iPhone Web 服务身份验证和访问 token