0

テーブルのビューセルでFontLabelを使用しています。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    FontLabel *author_name = [[FontLabel alloc] initWithFrame:CGRectMake(58, 10, 217, 16) fontName:@"MyriadPro-Bold" pointSize:12.0f];
    author_name.numberOfLines = 1;
    author_name.text = [dummyArray objectAtIndex:indexPath.row];
    author_name.textColor = [UIColor colorWithRed:0.580 green:0.776 blue:0.329 alpha:1.000];
    author_name.backgroundColor = [UIColor clearColor];

    [cell addSubview:author_name];
    return cell;

}

ただし、ラベルは複数回ロードされます。どんどん大胆になっていることがわかります。どうすれば修正できますか?

4

1 に答える 1

0

を呼び出すとaddSubview、古いビューは削除されません。セルは再利用されるため、最後の反復で追加したビューは引き続き存在することに注意してください。

UITableViewCell代わりに、を追加できるようにサブクラス化する必要がありますUILabel

編集:

またはあなたはすることができます

author_name.tag = 10;

その後

FontLabel *author_name = (FontLabel *)[cell.contentView viewWithTag:10];

あなたがそれを取得したいとき。

于 2012-08-07T21:16:47.740 に答える