Multiple columns in iOS Tableview

前端 未结 3 506
一生所求
一生所求 2021-01-06 12:32

How to create multiple columns in row of the tableview?

3条回答
  •  耶瑟儿~
    2021-01-06 12:49

    UITableView isn't really designed for multiple columns. But you can simulate columns by creating a custom UITableCell class. Build your custom cell in Interface Builder, adding elements for each column. Give each element a tag so you can reference it in your controller.

    Give your controller an outlet to load the cell from your nib:

    @property(nonatomic,retain)IBOutlet UITableViewCell *myCell;

    Then, in your table view delegate's cellForRowAtIndexPath method, assign those values by tag.

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *cellIdentifier = @"MyCell";
    
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      if (cell == nil) {
        // load cell from nib to controller's IBOutlet
        [[NSBundle mainBundle] loadNibNamed:@"MyTableCellView" owner:self options:nil];
        // assign IBOutlet to cell
        cell = myCell;
        self.myCell = nil;
      }
    
      id modelObject = [myModel objectAtIndex:[indexPath.row]];
    
      UILabel *label;
      label = (UILabel *)[cell viewWithTag:1];
      label.text = [modelObject firstField];
    
      label = (UILabel *)[cell viewWithTag:2];
      label.text = [modelObject secondField];
    
      label = (UILabel *)[cell viewWithTag:3];
      label.text = [modelObject thirdField];
    
      return cell;
    }
    

提交回复
热议问题