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
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
You must register your .xib file. In your viewDidLoad
method, add the following:
[self.tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:nil]
forCellReuseIdentifier:@"CustomTableCell"];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With