Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 custom cell not displaying in table view

Please bear with me, I am just starting iOS development. I am trying to display a custom tableViewCell within a storyboard tableView. I have done the following.

I have created a new .xib with a tableViewCell in it

enter image description here

I have then created a custom class for this. the .h file looks like this

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *thumbnailImageView;
@property (weak, nonatomic) IBOutlet UILabel› *titleLabel;

@end

Then in my TableViewController.m I have imported the CustomTableCell.h and I am doing following for 10 rows

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomTableCell";
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.titleLabel.text ="test text";


    return cell;
}

This seems fine to build, but when the project loads nothing happens. Any advice will be great.

I have placed a breakpoint in the cellForRowAtIndexPath method, however it never reaches this point. here is a screen shot of the simulator

enter image description here

like image 920
c11ada Avatar asked Nov 28 '22 03:11

c11ada


2 Answers

You must register your .xib file. In your viewDidLoad method, add the following:

[self.tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:nil] 
     forCellReuseIdentifier:@"CustomTableCell"];
like image 200
Steve Avatar answered Jan 11 '23 18:01

Steve


The way you are loading the nib is really old-fashioned and outdated. It is much better (since iOS 6) to register the nib and use dequeueReusableCellWithIdentifier:forIndexPath:.

See my explanation of all four ways of getting a custom cell.

like image 24
matt Avatar answered Jan 11 '23 19:01

matt