1

I want two custom (i.e. subclassed) UIViews in a subclassed UITableViewCell as shown in the below picture. The two UIViews are the same subclass.

enter image description here

Both the custom UIViews and the TableViewCell have associated xib's.

I would appreciate some advice on the best way to go about this. I load the TableViewCell this way.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"CustomCell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
   [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:NULL];
   // CustomCell is an IBOutlet connected to the above nib
   cell = BLCustomCell;
   }
   // configure the cell
}

I want to set outlets in the Custom Views to easily display data from my data model. Do I need a view controller for the Custom Views? I'm having trouble getting the nib to load for the Custom Views. (And yes, I realize my code above does not address this issue.) How do I get it to load? Does the TableView's controller need outlets to the Custom View objects?

Thanks!

Community
  • 1
  • 1
David Nix
  • 3,324
  • 3
  • 32
  • 51
  • Well it depends. What kind of information are you going to display on the customViews? Can you give an hint so I can understand you better. – Rui Peres Oct 07 '11 at 16:53

1 Answers1

2

The simplest way to handle complex UITableViewCells is to create a subclass of UITableViewCell, with its own IBOutlets that connect to the subviews, then just set properties of your custom cell in cellForRowAtIndexPath:. There are various other approaches, but this one seems to break down the problem reasonably well, and expands to handle more complex situations.

Take a look at the iOS Recipes book by Matt Drance, it cover this area well.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41