0

作成した行にデータを挿入しようとしています。ログにすべての情報を取得しますが、すべての行の最後の情報しか表示されません。誰かがこのエラーを回避する方法を提案できますか?

アドバイスをお願いします!

4

3 に答える 3

2

実際には、セルを再作成することはありません。最初に表示されるセルを作成し、同じコンテンツでそれらを再利用しています..以下を見てください:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
 *)indexPath
{
    static NSString *CellIdentifier = @"TestCell";

    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    // HERE YOU ONLY WANT TO INSTANTIATE THE CELL
        NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:nil options:nil];

        for (id currentObject in topObjects)
        {
            if([currentObject isKindOfClass:[TestCell class]])
            {
                cell = (TestCell *) currentObject;    
                break;
            }
        }
    }

    // HERE YOU WOULD ACTUALLY POPULATE THE CELL WITH DATA
    NSArray *array = [server get_texts:10 offset:0 sort_by:0 search_for:@""];

    NSMutableString *s = [[NSMutableString alloc] init];
    for (testMetaData *m in array){
        [s appendFormat:@"%@ %@ \n", m.title,m.note];

        cell.title.text = m.title;

        NSLog(@" title %@ ", m.title);

    }

    return cell;
}

についての情報UITableView:

したがって、適切に設定された tableView は、限られた数の のみを割り当てて使用しますUITableViewCell。割り当てた後、たとえば 5 つのセル (この数は「特定の時間に表示できるセルの数」によって決定されます)、表示領域からスクロールされた既に作成されたセルを取得し、それを返します。使用しているその方法で、再入力できます。したがって、cell変数はnilその時点ではなく、サーバー コードが呼び出されることはありません。

于 2013-10-18T23:24:51.577 に答える
0

forループに関係していると思います。

NSMutableString *s = [[NSMutableString alloc] init];
            for (testMetaData *m in array){
                [s appendFormat:@"%@ %@ \n", m.title,m.note];

                cell.title.text = m.title;

                NSLog(@" title %@ ", m.title);

            }

forループの最後でcell.title.text = m.title最後の情報を取得します。m.title

于 2013-10-19T02:30:42.983 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
 *)indexPath
{
//Load Cell for reuse
   static NSString *CellIdentifier = @"TestCell";
   TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
      cell =[ [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:nil options:nil] lastObject];
   }
//appending text and config cell
       NSArray *array = [server get_texts:10 offset:0 sort_by:0 search_for:@""];
       NSString *t = [array objectAtIndex:indexPath.row];
//Config cell - Not sure what you want. Maybe 10 different rows
        cell.title.text = t;
        return cell;

}
于 2013-10-21T16:31:53.707 に答える