从标题选择UITableview的多个部分

我正在尝试像树视图一样使用表视图来选择多个项目 您还知道其他方法来做树状视图吗?

我正在尝试向不同提供商的相机用户授予权限

如何从标题部分的UIButton中选择所有子级

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

  CGRect frame = tableView.frame;

  UIButton *selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0,-5,40,40)];
  [selectButton setImage:[UIImage imageNamed:@"ic_combobox_off"] forState:UIControlStateNormal];
  
  [selectButton addTarget:self action:@selector(headerSelected:) forControlEvents:UIControlEventTouchUpInside];

  UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(40,200,30)];
  title.text = [_dataProviders objectAtIndex:section];

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,frame.size.width,frame.size.height)];
  [headerView addSubview:selectButton];
  [headerView addSubview:title];
  [headerView setBackgroundColor:UIColor.lightGrayColor];
  return headerView;
  }

- (void) headerSelected:(NSInteger) section { }

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  
  VideoPermissionsServicetableViewCell *cell = (VideoPermissionsServicetableViewCell*) [tableView dequeueReusableCellWithIdentifier:CELL_VIDEO_PERMITION forIndexPath:indexPath];
  
  NSString *sectionTitle = [_dataProviders objectAtIndex:indexPath.section];
  NSArray *sectionDevice = [_dataInput objectForKey:sectionTitle];
  NSString *device = [sectionDevice objectAtIndex:indexPath.row];
  
  cell.deviceLabel.text = device;
  
  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  return cell;
  }
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  NSString *providers = [_dataProviders objectAtIndex:section];
  NSArray *devices= [_dataInput objectForKey:providers];
  return [devices count];
}

iCMS 回答:从标题选择UITableview的多个部分

首先,您的代码中存在很多问题:

  1. 将UITableViewHeaderFooterView用于页眉和页脚视图。这样可以节省内存。有很多示例和教程;
  2. 在cellForRowAtIndexPath()中,除非在视图控制器加载时间中的某个位置注册了单元笔尖,否则dequeueReusableCellWithIdentifier()之后的单元格可以为nil。
  3. headerSelected()采用事件发送者的输入参数,而不是部分编号。在您的情况下,它将是您的UIButton。在这种方法中,您应该从按钮本身中找到节号。一个好主意是在填充标题视图时将部分设置为按钮。尝试使用标签属性;
  4. 在具有节号的简单情况下,您可以遍历该节中的单元格,通过cellForRowAtIndexPath()获取该单元格,访问其按钮,并将其设置为新状态。

请注意,您只能获得可见的单元格。为了使其适用于向下滚动的用户,您需要他们在cellForRowAtIndexPath()中填充单元格期间,根据部分按钮状态设置按钮状态。

本文链接:https://www.f2er.com/2072387.html

大家都在问