0

I've created a custom cell class using these instructions: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

Now, I have two view controllers: MyPostsViewController and CustomCellViewController. The custom cell is just a cell with a UITableViewCell and has two labels declared and connected. The MyPostsViewController is a UITableView that uses these custom cells. However, I am unsure how to access the two labels of the CustomCellViewController from the MyPostsViewController. I want to access them from the MyPostsViewController because that is where the cellForRowAtIndexPath method is and where I want to set the value of my labels at. How would I do this?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Snowman
  • 31,411
  • 46
  • 180
  • 303

3 Answers3

1

You don't need a CustomCellViewController if you follow the instructions you posted. Just load your cells from your NIB in your MyPostsViewController.

To access individual subviews of your cell, give them each a distinctive tag, then retrieve them with [cell viewWithTag:42], for example.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • I'm sorry but I'm not really understanding-how would I have done this otherwise? I just followed the instructions for the second approach. – Snowman Aug 19 '11 at 02:48
  • By using the second approach (`NSBundle:loadNibNamed:owner:options:`) you don't need such a `CustomCellViewController`... – Cyrille Aug 19 '11 at 02:51
  • Take a look at this: http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files/541425#541425 – Cyrille Aug 19 '11 at 02:51
  • So where should my custom cell be set up? In the MyPostsViewController.xib file? I'm not seeing the solution in these links.. – Snowman Aug 19 '11 at 02:54
  • And this one? http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW20 – Cyrille Aug 19 '11 at 02:57
1

You should customize (set the values for your labels) in cellForRowAtIndexPath:. To do this, you can set tags for your labels in IB itself (find the tag field in the Attributes inspector). Let's say you set 1 for Label1 & 2 for Label2. Then, your code would look something like (copying from the link you posted)-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

//customize the cell here
UILabel* label 1 = [cell viewWithTag:1];
label1.text = @"my text";

//similarly label 2

    return cell;
}

HTH,

Akshay

Akshay
  • 5,747
  • 3
  • 23
  • 35
1

I think the cleanest way to do this is to define IBOutlets on your custom UITableViewCell subclass. Then, when designing your XIB, CTRL+click your custom cell. You should see the outlets there. Drag-drop to hook up your outlets to your labels, just like you normally would with views. Finally, access those IBOutlets in your cellForRowAtIndexPath method.

Nathanial Woolls
  • 5,231
  • 24
  • 32