Reputation: 2819
I am using a custom UITableViewCell in my application to display a simple label for each row. However, for some reason I am unable to display the contents of the array that contains the text I want to display in each cell. I made my custom UITableViewCell in a .xib file using Interface Builder, and my viewController that is using this custom UITableViewCell is inside my storyboard file.
Here is what my screen looks like in IB:
Here is the relevant method inside my custom UITableViewCell class:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
Because I've done everything inside IB, I didn't include any code inside the initWithStyle method.
My relevant code inside my ViewController where I am using the custom cell is as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.menuArray = @[@"Apples", @"Pears", @"Bananas", @"Oranges", @"Peaches"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = (MenuTableCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.menuArray count];
}
Can anyone see what it is I am doing wrong?
Upvotes: 0
Views: 508
Reputation: 3416
I Think This Will Work for You.... And Also Check your Label Outlet Connection For Displaying Proper Answer
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [menuArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
}
Upvotes: 0
Reputation: 104092
If you make a cell in a xib file, you should call registerNib:forIdentifier: in viewDidLoad. If you do this, it's unnecessary to check for a nil cell in cellForRowAtIndexPath.
Upvotes: 1
Reputation: 1345
Implement the required methods below in your viewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
And I have edited the cellForRowAtIndexPath have a look at it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Edited Code for creating the cell*****************************************
if (cell == nil)
{
NSArray *objectsArray = [[NSBundle mainBundle] loadNibNamed:@"MenuTableCell" owner:self options:nil];
for (id objectCell in objectsArray)
{
cell = (MenuTableCell*)objectCell;
}
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
return cell;
}
Hope this works for you
Upvotes: 0
Reputation: 3226
Add numberOfRowsInSection
method in yourViewController
like below
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
Upvotes: 0