iosobjective-cuitableviewuitableviewsectionheader

Multiple section from header selection UITableview


i´m trying to do like a treeview with a tableview to select multiple items do you know any other way to do a treeview?

I'm trying to give permissions to the user of cameras from different providers

How can i select all childrs from a UIButton on the header section

- (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, 0, 200, 30)];
  title.text = [_dataProviders objectAtIndex:section];

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 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];
}


Solution

  • First off, lots of problems in your code:

    1. Use UITableViewHeaderFooterView for header and footer views. This saves memory. There is a lot of examples and tutorials;
    2. In cellForRowAtIndexPath(), your cell after dequeueReusableCellWithIdentifier() can be nil, unless you register the cell nib somewhere in view controller load time;
    3. headerSelected() takes input param of the sender of the event, not section number. In your case it will be your UIButton. In this method, you should find the section number from the button itself. Good idea is to set the section to the button during the header view populating. Try to use tag property;
    4. In simplistic case, having section number, you can traverse over the cells in that section, get the cell by cellForRowAtIndexPath(), access its button, and set it the new state.

    Note, that you can get only visible cells. To make it work for those down the scroll, you need them to set the button state according with the section button state during the cell populating in cellForRowAtIndexPath().