Reputation: 245
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:
Linked the datasource and delgate
Linked the IBOutlet for the table view
Set the cell identifier to Cell
Made sure the array was not nil (the log gives the right number)
Tried using a static NSMutableArray, and it worked, but once i went back to my dynamic array it did not
"Was Called" never appears in the log
Upvotes: 0
Views: 260
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
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
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