0

Currently I have the following method, but it doesn't quite work...

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

    static NSString *MyIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
        cell = tvCell;
        self.tvCell = nil;
    }

    UILabel *label;
    label = (UILabel *)[cell viewWithTag:5];
    label.text = [NSString stringWithFormat:@"Hole #%d", indexPath.row];

    return cell;
}

The table view gets created with no errors, but each individual cell contains nothing, but clearly the TVCell.xib has a label with a tag of 5. The only question I have is this. I don't quite understand these steps apple gives here...

  1. Select File’s Owner in the nib document window, open the Identity pane of the inspector, and set the class of File’s Owner to your custom view controller class.

  2. Connect the cell outlet of File’s Owner (now the placeholder instance of your custom subclass) to the table-view cell object in the nib-file document.

Here is where those steps are... http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

Can someone please explain those steps for a noob like me? I think that is what I messed up on, but I could have done anything wrong.

The Man
  • 1,462
  • 3
  • 23
  • 45

3 Answers3

1

I don't think TcCell becomes a property of self. Try this instead when the cell queue is empty:

if (cell == nil) {
    cell=[[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
}
user1258240
  • 855
  • 7
  • 15
  • you are right, i just checked it. @jonkroll seems to have a more correct version. Also, you can write cell=self.myViewFromNib after you call the mainBundle: this is suggested on another thread here [http://stackoverflow.com/questions/863321/iphone-how-to-load-a-view-using-a-nib-file-created-with-interface-builder] – user1258240 Apr 06 '12 at 00:31
0

It's kind of difficult to explain the steps that you said you don't understand in words. I would suggest looking for tutorials that have images or videos for that. (I did a quick search and there are lots available, but I didn't really want to choose one to direct you to without having read them more closely).

However, I prefer to create custom table view cells this way: http://www.mobilesce.com/2011/12/27/the-best-way-to-do-custom-reusable-uitableviewcells/

It's slightly different from the way described in the apple docs, but I've seen a lot of people use it and I find it easier.

Darren
  • 10,091
  • 18
  • 65
  • 108
  • That example is not quite working for me... Where does he get this... MenuItem *menuItem = [menuItems_ objectAtIndex:[indexPath row]]; --- What is the "MenuItem" variable type? – The Man Apr 06 '12 at 01:11
  • MenuItem is just a generic object that is used to populate the cells in that particular example. You can remove it and just assign what ever values you want to the cell properties. – Darren Apr 06 '12 at 01:54
  • Thanks, I got it to work, but the table itself is messed up now. The table looks empty at first, but if you start scrolling my table cells come in from every where. – The Man Apr 06 '12 at 12:40
  • Hmm...maybe you could post your updated tableView:cellForRowAtIndexPath: method? When you say that when you start scrolling your cells come in from everywhere what do you mean? Do they all fill in immediately, or is it just new cells that scroll onto the screen get filled in? Also, what do you have in your dataSoure tableView:numberOfRowsInSection: method? – Darren Apr 06 '12 at 13:15
0

The NSBundle method loadNibNamed:owner:options: returns an NSArray containing the top level objects in your nib file. Try this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
    cell = [nibArray objectAtIndex:0];
}
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • Have you checked to see that your tableView:cellForRowAtIndexPath: method is actually getting called? Have you made your view controller the table view's data source? – Darren Apr 06 '12 at 00:41