0

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

我将消息存储在 中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没有正确完成。

4

1 回答 1

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正确 实施

于 2012-06-18T08:54:31.267 回答