聊天在所有单元格中加载相同的消息

时间:2012-06-18 08:51:57

标签: ios xmppframework

我正在开发一个需要聊天功能的应用,在表格视图中显示发送和接收的消息。我能够毫无问题地加载第一条消息,但是当第二条消息到达时,第一条消息在表中被删除并呈现第二条消息,但是在两个单元格中,第三条消息在三个单元格中,依此类推。

我将消息存储在NSMutableArray中,然后运行该数组:

-(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;

}
}

就像for未正确完成一样。

1 个答案:

答案 0 :(得分:0)

您的for循环将最后一条消息分配给当前单元格 你要做的是

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    General *general = [General sharedManager];

    NSLog(@"We are in cellForRowAtIndexPath de ChatViewController");
    messarray = general.messarray;

    //Get the element basing on the current row
    diccio=[messarray objectAtIndex:indexPath.row];
    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;
}

建议正确实施dequeueReusableCellWithIdentifier