SoFunction
Updated on 2025-04-13

iOS development tableView cell implementation code

1. Implementation method

For example, friends grouping is divided into two groups: friends and strangers, realizing the function of clicking on friends and strangers to expand or retracting the corresponding cell of the group.

Implementation: You can group the sections of the corresponding tableView, click the section to expand and retract the cell.

Create a temporary array selectedArr to store the section that needs to be expanded. Click section to determine whether selectedArr contains the group. If included, remove it, and if not included, add it to selectedArr.

Shows the selectedArr containing the cell.

2. Code implementation

#import ""

@interface ZCellGroupController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;//Group@property (nonatomic, strong) NSArray *friendsArray;//The corresponding content of each group@property (nonatomic, strong) NSMutableArray *selectedArr;//Storage the cell group that needs to be expanded
@end

@implementation ZCellGroupController


-(void)viewDidLoad {
  [super viewDidLoad];
   = [[NSMutableArray alloc]init];
  [self addTableView];
  [self addData];
}
- (void)addTableView{
  UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, kScreenHeight) style:UITableViewStyleGrouped];
   = tableView;
   = self;
   = self;
   = UITableViewCellSeparatorStyleSingleLine;
  [ addSubview:_tableView];
}
- (void)addData{
   = [NSArray arrayWithObjects:@"Friends",@"stranger", nil];
   = [NSArray arrayWithObjects:
                       @[@"A",@"B",@"C",@"D",@"E",@"F"],
                       @[@"Stranger 1",@"Stranger 2",@"Stranger 3",@"Stranger 4",@"Stranger 5",@"Stranger 6",@"Stranger 7",@"Stranger 8",@"Stranger 9",@"Stranger 10",@"Stranger 11",@"Stranger 12",@"Stranger 13",@"Stranger 14"],nil];
}

#pragma mark --tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)section];
  NSInteger num ;
  //If selectedArr does not contain section, the grouping returns number 0;  if ([ containsObject:sectionStr]) {
    NSArray *arrayData = [section];
    
    num =  ;
  }else{
    num = 0;
  }
  return num;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
   = [UIColor whiteColor];
  UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5,kSCREENWIDTH-20 , 30)];
   = [section];
  [view addSubview:titleLabel];
  
  //Add a click event  UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
   = 100+section;
  [btn addTarget:self action:@selector(viewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  [view addSubview:btn];
  return view;
}
- (void)viewBtnClick:(UIButton *)btn{
  NSString *string = [NSString stringWithFormat:@"%ld", - 100];
  if ([ containsObject:string]) {
    [ removeObject:string];
  }else{
    [ addObject:string];
  }
  NSLog(@"selectedArr:%@",);

  [_tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)];
  static NSString *cellID = @"testCellID";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
     = UITableViewCellSelectionStyleGray;

  }
  
  NSArray *arrayData = [];
  
  if ([ containsObject:sectionStr]) {
     = arrayData[];
  }
  return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 45;
}
@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.