Matt Sarabyte
Matt Sarabyte

Reputation: 245

Table View's cellForRowAtIndexPath: not being called

For some reason, my table view's cellForRowAtIndexPath:(NSIndexPath *)indexPath method isn't being called. Here is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    messages = [[NSMutableArray alloc]init];
    chatTable = [[UITableView alloc]init];
    chatTable.delegate = self;
    chatTable.dataSource = self;
}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Called Count %i",messages.count);
    return messages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Was Called");

    static NSString *cellId = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    NSDictionary *message = [messages objectAtIndex:indexPath.row];
    NSString *mess = [message objectForKey:MSChatMessage];
    cell.textLabel.text = mess;

    return cell;
}

Here's what I've done:

Upvotes: 0

Views: 260

Answers (3)

krushnsinh
krushnsinh

Reputation: 864

i hop these helps..

in .h file..

NSMutableArray * messages;

in .m file..

- (void)viewDidLoad 
{
 [super viewDidLoad];
 // Initialization of NSMutableArray. 
 messages = [[NSMutableArray alloc]init];
}

and make sure its not nil in here..

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 NSLog(@"Called Count %i",messages.count);
 return messages.count;
}

Upvotes: 0

Dhiraj
Dhiraj

Reputation: 75

When you have linked the UITableView from xib or storyboard as IBOutlet. Why are you creating the UITableView again "chatTable = [[UITableView alloc]init]" Remove this line from the code and try. Also set the delegate and datasource from the Interface Builder and remove chatTable.delegate = self; & chatTable.dataSource = self;

Also observed that you are creating the message in viewDidLoad which is not proper. Try to print this count of messages, it should not be zero.

Upvotes: 1

meda
meda

Reputation: 45500

You initialized message:

messages = [[NSMutableArray alloc]init];

But then you never populated the array.

cellForRowAtIndexPath delegate is called based on the datasource array count.

Make sure the following line:

NSLog(@"Called Count %i",messages.count);

prints a count greater than 0

Upvotes: 2

Related Questions