0

テーブル ビューで送受信されたメッセージを表示するチャット機能を必要とするアプリを開発しています。問題なく最初のメッセージを読み込むことができますが、2 番目のメッセージが到着すると、最初のメッセージはテーブルで消去され、2 番目のメッセージが表示されますが、2 つのセルに、3 番目のメッセージが 3 つのセルに表示されます。

メッセージを に保存してから、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 に答える