1

I have a problem settings my view elements on a custom cell. The table cells appear in my tableView, but the properties do not set and thus only empty/blank cells appear.

The tableView is not a tableView controller, but only a tableView in a viewController.

I have the following files:

CustomCell.xib:
Here i use IB to build the custom cell by using a Table View Cell from object library with labels and images on. I set the Identifier as orderListCell. From this screen I ctrl+drag to create the outlets in customCell.h

CustomCell.h
Here I see all my IBOutlets as properties from above mentioned file

CustomCell.m
Here I leave as is

OrderListViewController.h
Here I import customCell.h and use protocols UITableViewDelegate, UITableViewDataSource

OrderListViewController.m
Here I set my tableView delegate and tableView dataSource to self. I also create an IBOutlet for my tableView from the Storyboard.

I use the following code to try and display my cells:

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

static NSString *cellIdentifier = @"orderListCell";

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
    cell = [[CustomCell  alloc] init];
}

 cell.myLabel.text = @"aaaaaaaa"; //[[self.orders objectAtIndex:indexPath.row] objectForKey: @"tableNo"];



return cell;
}

I have simplified my code a bit to demonstrate that even setting the label to a simple string (@"aaaaaaaa") doesnt work. When I look at the objects in my debugger the cell does have all the properties from the IBOutlets and the cell does appear in my tableView, just the label.text = xxx does not seem to work.

I have looked at the following posts but either dont understand it properly or it does not work for me:

ios 7 customizing UITableViewCell's content view
Can't set properties in Custom UITableViewCell
Set label for a custom cell

Any help or advice would be greatly appreciated

1 Answer 1

2

cell = [[CustomCell alloc] init]; does not create your cell from the XIB, so none of the XIB content will be created.

Use registerNib:forCellReuseIdentifier: to register the XIB with the table view so that it will create your cell instances for you (you don't need to create instances yourself in cellForRowAtIndexPath).

If you don't want to do that, you can load your NIB explicitly with nibWithNibName:bundle: and instantiateWithOwner:options:, then get the cell instance from the list of returned views (which should only contain 1 item).

Sign up to request clarification or add additional context in comments.

1 Comment

Wow this issue really frustrated the life out of me. Your response put me on the right track and after some research and trial and error I finally figured it out. Before using registerNib:forCellReuseIdentifier: one first need to create the nib using [UINib nibWithNibName:@"MyCustomCellName" bundle:nil] and then use that in the first mentioned method. Thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.