Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS : Customizing TableViewCell - Initializing Custom Cell

In my TableView, I have a dataSource of NSMutableArray *currList - it contains objects of object Agent. I created customized TableCell and set up everything properly. I am finding problem while displaying data :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *simpleTableIdentifier = @"ChatListCell";

     // Custom TableViewCell
     ChartListCell *cell = (ChartListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

     if (cell == nil) {
           // I believe here I am going wrong
           cell = [[ChartListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
           NSLog(@"Cell = %@", cell);  // Shows null
     }
     /*
     With UITableViewCell, things are working perfectly fine
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
     if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
     }
     */
     Agent *agent = (Agent *)[currChatList objectAtIndex:indexPath.row];
     NSLog(@"Agent name - %@", agent.name);   // Prints proper data
     cell.nameLabel.text = agent.name;
     cell.thumbImageView.image = [UIImage imageNamed:agent.photo];
     cell.timeLabel.text = agent.chatTime;

     return cell;
}

As you can see in above code comments, If I comment the custom ChartListCell and use UITableViewCell, it works properly. But with ChartListCell, nothing comes up and in logs I get "Cell = null" & Agent name is showing properly. Cell shouldn't be null. Why is it null, can anyone please help me out with this. Where am I doing mistake ?

Any help is highly appreciated.

Thanks

like image 923
Tvd Avatar asked Feb 28 '14 20:02

Tvd


1 Answers

import your custom cell file and try this

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

static NSString *simpleTableIdentifier = @"ChartListCell";

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

}         

 Agent *agent = (Agent *)[currChatList objectAtIndex:indexPath.row];
 NSLog(@"Agent name - %@", agent.name);   // Prints proper data
 cell.nameLabel.text = agent.name;
 cell.thumbImageView.image = [UIImage imageNamed:agent.photo];
 cell.timeLabel.text = agent.chatTime;

return cell;
}
like image 141
Mohit Avatar answered Oct 13 '22 00:10

Mohit