1

I am having an interesting problem creating the custom tableview I need...

I found this question, but it does not really address my issue...

I am trying to put a subclassed UIView inside a subclassed UITableViewCell. The custom view holds a UIButton and a couple labels. Simplified, it's like this:

image of view hierarchy

Both the custom view and custom tableviewcell have xibs.

MyCustomView.xib's class is set to MyCustomView and I have properties for the labels and the button as well as an IBAction for the button.

MyCustomTableView.xib has a property for MyCustomView and is importing MyCustomView.h.

In MyCustomView.xib, I have this init:

-(id)initWithNibName:(NSString *)nibName nibBundle:(NSBundle *)nibBundle myLabelText:(NSString *)labelText {
    //The custom view in the tableviewcell is 69 by 64...
    if ((self = [super initWithFrame:CGRectMake(0, 0, 69, 64)])) {
        [self setmyLabelText:labelText];
    }
    return self;
}

And in my TableViewController...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomTableViewCell *cell = (MyCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"theCell" forIndexPath:indexPath];

    // Configure the cell...
    cell.customView = [[MyCustomView alloc] initWithNibName:@"MyCustomView" nibBundle:nil fileContentID:@"Some Text..."];

    return cell;
}

When I run the app, the custom tableview cell's contents are fine, but the content of the custom view inside the custom tableview cell is blank.

Community
  • 1
  • 1
BBruce
  • 265
  • 6
  • 15
  • Forget it, I'll delete the comment, didn't understand you correctly, it's quite late here in Berlin ;) Delete yours too – Julian F. Weinert Dec 15 '14 at 01:29
  • 1
    You can't set the text of your label in initWithNibName, because the outlets are not connected yet. Also, you never add your custom view to your cell, you just alloc init it. You're also going to get in to trouble doing that in cellForRowAtIndexPath any way, because you will be adding a new view whenever the cell is reused. You really ned to rethink your approach. – rdelmar Dec 15 '14 at 02:05
  • why you are making too complicated ? in storyboard create a customized tableviewCell with your requirement design in your tableView. then create a UItableViewCell subclass and connect to cell. Then, connect IBoutlet to the views. – Vineesh TP Dec 15 '14 at 04:29

2 Answers2

1

It seems that MyCustomView's initializer(-initWithNibName:nibBundle:myLabelText:) don't load any xib. This post will help you.

How to load a xib file in a UIView

...and MyCustomView should be created once inside MyCustomTableViewCell, as @rdelmar says.

Community
  • 1
  • 1
bluedome
  • 2,449
  • 1
  • 22
  • 17
  • Thanks. That has been updated to recommend container views as well. Good reading. I'll do some experimentation and see which works better for my app. – BBruce Dec 16 '14 at 01:18
0

You need to do most of the formatting work in MyCustomTableViewCell - I would not use a XIB and code the views directly because that class is called many times. Apple has number of sample codes regarding TableViewCells - One of them I believe is called Elements that use fancy tableview cells for the Elements of the Periodic Table. Most of my apps use custom cells with icon images and I started with that sample code many years back (since IOS 4).

Your CellForRowatIndexPath should just be passing the image and the label text to your tableviewCell Class instance. If you have question just ask - but I am sure that sample code from apple is sufficient to get you started.

Paulo
  • 1,245
  • 10
  • 8
  • Thanks. I am comfortable with custom cells built out in the tableview directly rather than a xib for the cell. I was just hoping to compartmentalize my code structure a bit more. – BBruce Dec 16 '14 at 01:16