Question

I need to present my table view with a two column list. I read some related posts about making some grid by overriding drawRect (here). However, I'm seeking a simple way to design my cell with IB in nib, then load it and push two cells on each row. The example with drawRect is not appropriate because it involves manual setting of positions. I just need to push those two cells with some autosizing, that's it. Is it possible?

I'm looking for something like (in cellForRowAtIndexPath):

cell.contentView = emptyUIViewContainer;
[cell.contentView addSubview:FirstColumnUIView];
[cell.contentView addSubview:SecondColumnUIView];

I don't need to have two separate nibs for those two columns because every column is of the same format just with some other data. Any ideas?

UPDATE: Intuitively, I'm trying to do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell1 == nil) {
    cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  // Configure the first cell.
  cell1.textLabel.text = ...some text

  // Configure the second cell
  UITableViewCell *cell2 = [[UITableViewCell alloc] init];
  cell2.textLabel.text = ...some text

  //set row as container for two cells    
  UITableViewCell *twoColumnRowView = [[UIView alloc] init]; //initWithFrame:CGRectMake(0, 0, 200, 20)];

  cell1.contentView.frame = CGRectMake(0, 0, 100, 20);
  [twoColumnRowView addSubview:cell1];
  cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
  [twoColumnRowView addSubview:cell2];

  return twoColumnRowView; // cell;
}

It's just a raw prototype I'm playing with now. But the code crashes at runtime with "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTableViewStyle:]: unrecognized selector sent to instance"

UPDATE 2. I have changed a code to look more practical. Strange but after several tries I got the app working without crash but with weird black background in all cells. Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *FirstCellIdentifier = @"FirstCellIdentifier"; 
  static NSString *SecondCellIdentifier = @"SecondCellIdentifier";

  // initialize first cell
  UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:FirstCellIdentifier];

  if (cell1 == nil) {
    cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstCellIdentifier] autorelease];
  }

  //initialize second cell
  UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];

  if (cell2 == nil) {
    cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SecondCellIdentifier] autorelease];
  }

  cell1.textLabel.text = ...data
  cell2.textLabel.text = ...data

  UITableViewCell *twoColumnRowView = [[UITableViewCell alloc] init];

  [twoColumnRowView addSubview:cell1];
  //cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
  [twoColumnRowView addSubview:cell2];

  return twoColumnRowView; // cell;
}

I'm not reusing twoColumnRowView but both other does do.

Was it helpful?

Solution

You can't (or at least, shouldn't) put two table views next to each other. And, as you already know, a table view only ever has a single column.

However, you can put as much data as you want in each cell.

You can do most of this in Interface Builder. Create a XIB with a UITableViewCell. Drag and drop a couple of UILabels into the right place. You could update the labels by finding them with the viewWithTag: method, but it would be better to create a custom UITableViewCell class with properties pointing to each label.

Since tables are so central to UIKit, there are a lot of samples in Apple's documentation set and some good WWDC talks. You can download the videos from iTunes.

OTHER TIPS

Hope this will help you...

http://www.iphonedevx.com/?p=153

else the best way is to put two table views side by side, if they need to scroll independently.

Hope this will help you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top