How to create multiple columns in row of the tableview? it would be grateful if give any samples or please direction me!
+3
A:
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;
}
chrispix
2010-07-15 06:03:05
Thanks for your valuable reply. but i need as DataGrid like matrix table view with header and records..
sri
2010-07-15 06:32:49
Maybe I don't understand what you're looking for, but you could easily use this approach to create something that looks like an excel table. You could add a view to the section header for your headers. If you're looking for an easy solution that automatically creates a view that looks like a matrix which you can just populate with data, I don't know of anything.
chrispix
2010-07-15 16:54:36
@chrispix, I'd like to replicate this for myself, can you elaborate on the type of class this custom cell should be in IB? In order for that new cell you load up for each row to map to your outlet, does the MyTableView NIB contain a table view cell of a custom class that inherits from UITableViewCell and has a MyCell outlet? Your use of owner:self here seems to suggest that the cell loaded is actually of type UITableViewController, which seems odd.
Joost Schuur
2010-07-15 17:15:03
First, give your UITableViewController an IBOutlet for the cell. In my example, @property(nonatomic,retain) IBOutlet UITableViewCell *myCell.In IB, drag out a Table View Cell from the library. Drag out other components (UILabels in my example) as subviews of the cell, and give each one a unique Tag value in the attributes inspector. Make the File's Owner your controller and associate the outlet with your cell.You don't need a subclass of UITableViewCell. In my example, the loadNibNamed method is loading the contents of the root view into the myCell outlet on the UITableViewController.
chrispix
2010-07-16 06:14:19
A:
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. ;-)
Sandro Meier
2010-07-15 07:09:45