How to create multiple columns in row of the tableview?
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 :)
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;
}
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. ;-)