Understanding of tableView and use of tableViewCell

Format difference of tableView

Select the UITableViewStyleGrouped mode to display all of them. When the UITableViewStylePlain is pulled up, the top of the group is at the top
When using the UITableViewStyleGrouped style, a headView will appear on it, accounting for about 35 pixels.
tableView.tableHeaderView =
[[uiview alloc initwithframe: cgrectmake (0, 0, 0, cgfloat? Min)]; / / cgfloat? Min a macro,
Can remove the head view

Auxiliary view of tableViewCell##

cell.accessoryType = UITableViewCellAccessoryNone;//cell has no style

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//There is a small arrow on the right side of the cell, which is more than ten pixels away from the right side;

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//There is a blue round button on the right side of the cell;

cell.accessoryType = UITableViewCellAccessoryCheckmark;//The shape on the right of the cell is a check mark

No click effect when clicking cell

cell.selectionStyle = UITableViewCellSelectionStyle.None

Hide split lines

tableView.separatorStyle = UITableViewCellSeparatorStyle.None

Hide redundant cell s

tableView.tableFooterView = UIView(frame: CGRectZero)//Hide the extra cell s below

Reuse of cell

For a group of cells, if you want to change the background color of one cell, tag the cell with its tag, for example

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     cell.myLabel.text = [NSString stringWithFormat:@"My Label%ld", indexPath.row];
      cell.tag = indexPath.row;
      if (cell.tag == 5) {
          cell.imageVIew.backgroundColor = [UIColor greenColor];
      if (cell.tag != 5) {        
      cell.imageVIew.backgroundColor = [UIColor whiteColor    

     return cell;

If it is not marked, use indexPath.row to determine that reusing cells will cause other cells to be reused because they are the same reuseIdentifier.

Here is a way to customize cell s
Note that a cell reuse identifier with a reuse identifier

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    NSString *str = @"cell";

    if(indexPath.section == 0){
        UITableViewCell *cell01 = [_tableView dequeueReusableCellWithIdentifier:@"cell01"];
        if(cell01 == nil){
            cell01 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell01"];
           // cell01.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(135, 70, 50, 25)];
            btn2.layer.masksToBounds = YES;
            btn2.layer.borderWidth = 0.9;
            btn2.layer.borderColor = [UIColor grayColor].CGColor;
            btn2.layer.cornerRadius = 10;
            [btn2 setTitle:@"Lv10" forState:UIControlStateNormal];
            [btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(330, 20, 80, 40)];
            btn.layer.masksToBounds = YES;
            btn.layer.borderWidth = 0.5;
            btn.layer.borderColor = [UIColor grayColor].CGColor;
            btn.layer.cornerRadius = 5;
            [btn setTitle:@"Sign in" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"Sign in map"] forState:UIControlStateNormal];
            [btn setTitle:@"Check-in" forState:UIControlStateSelected];
//            [btn setImage:nil forState:UIControlStateSelected];

            [btn addTarget:self action:@selector(pressRight:) forControlEvents:UIControlEventTouchUpInside];

            [cell01.contentView addSubview:btn2];
            [cell01.contentView addSubview:btn];
        cell01.textLabel.text = @"Crisscross footpaths";
        cell01.imageView.image = [UIImage imageNamed:@"Head portrait.jpeg"];
        return cell01;

Posted on Thu, 09 Jan 2020 11:36:16 -0800 by TobyRT