2

私のuitableviewには2つのセクションがあります

最初はコアデータからフェッチされます

その他は、NSMutableArray(otherFriends)に格納されているテキストフィールドを介して追加されます

これが私のコードです

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([otherFriends count]>0)
    {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    if(otherFriends == 0)
    {
        return [[[[self fetchedResultsController]sections]objectAtIndex:0]numberOfObjects];
    }
    return [otherFriends count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newGroupCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:20.0];
    cell.textLabel.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];

    if(indexPath.section == 0)
    {
        user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

        cell.textLabel.text = user.name;
        cell.detailTextLabel.text = user.primaryResource.status;

        [cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
        [cell.imageView.layer setMasksToBounds:YES];

        if (user.photo != nil)
        {
            cell.imageView.image = user.photo;
        }
        else
        {
            cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
        }
    }
    else
    {
        cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
    }

    return cell;
}

最初のセクションにも字幕がありますが、2番目のセクションのセルには字幕がありません

新しい友達を追加するときは、すべての行が表示されるまで正常に機能しますが、新しい友達を追加してその行が表示されず、その行を表示するにはスクロールする必要があります。この行には、セクション0の最初の行のサブタイトルが表示されます(非常に最初のセル)とセクション1の3行目で繰り返されます。字幕だけが繰り返されていますが、本文は繰り返されていません。

数時間の間、私はこれを理解しようとしていますが、運がありません。

4

2 に答える 2

2

これは、elseブランチでを設定していないためcell.detailTextLabel.textです。

セルがリサイクルされると、古いdetailTextLabelはそこに残ります。セルがリサイクルされた可能性がある場合は、条件の両方のブランチにセルのすべてのプロパティを設定する必要があります。

if(indexPath.section == 0)
{
    user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    cell.textLabel.text = user.name;
    cell.detailTextLabel.text = user.primaryResource.status;

    [cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
    [cell.imageView.layer setMasksToBounds:YES];

    if (user.photo != nil)
    {
        cell.imageView.image = user.photo;
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
    }
}
else
{
    cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
    // ADDED
    cell.detailTextLabel.text = @"";
    // You may also need to adjust the frame of the cell.imageView
    // because it could have been recycled.
}
于 2013-01-18T15:51:38.317 に答える
0

セルは再利用されます。両方のセクションで同じセルを使用しているため、すべての条件で同じセルプロパティのセットを設定/リセットする必要があります。

問題は、セルがセクション1に使用されている場合、をリセットしないことですdetailTextLabelelseブロックに次を追加します。

cell.detailTextLabel.text = nil;

これにより、すべての場合で、再利用されたセルの、、、およびプロパティをtextLabel設定detailTextLabelすることが保証されます。imageView

于 2013-01-18T15:51:44.597 に答える