Multiple columns in iOS Tableview

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

How to create multiple columns in row of the tableview?

相关标签:
3条回答
  • 2021-01-06 12:48

    I have created UIGridView in which a single UITableViewCell contains more than one cells. You can learn the source code of UIGridView.

    My advice is not to code it in your UIViewController. Build an external control and use it in UIViewController is a much better way.

    Or you can just use mine :)

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-06 12:52

    You could use multiple UITableViews side by side. That should look ok with custom cells. So you can transform it until it looks like a DataGrid. ;-)

    0 讨论(0)
提交回复
热议问题