I am developing an app that needs the chat functionality, presenting messages sent and received in an Table View. I am able to load the first message without any problem, but when a second message arrives, the first one is erased in the table and presents second message, but in two cells, the third messages in three cells and so on.
I am storing the messages in an NSMutableArray, and then I run that array:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
General *general = [General sharedManager];
NSLog(@"We are in cellForRowAtIndexPath de ChatViewController");
//UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
//if(!cell){
messarray=general.messarray;
for (int i=0;i<=messarray.count; i++)
{
diccio=[messarray objectAtIndex:i];
general.firstmess=[diccio objectForKey:@"msg"];
general.firstfrom=[diccio objectForKey:@"sender"];
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
//}
text=general.firstmess;
remite=general.firstfrom;
[[cell textLabel]setText:remite];
[[cell detailTextLabel] setText:text];
return cell;
}
}
Its like the for is not done properly.
Your for loop assings the last message to the current cell
What you have to do is
a suggestion is to implement
dequeueReusableCellWithIdentifiercorrectly