1

テーブルビューをスクロールすると、テキストが下のセルからすべてマッシュアップされます。cellForRow が読み込まれるたびに UILabels を再作成するのはおそらく原因ですが、修正方法はわかりません。

 (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];

    NSDictionary *currentRowDictionary = nil;
    if (tableView == [[self searchDisplayController] searchResultsTableView])
        currentRowDictionary = [[self searchResults] objectAtIndex:row];
    else
        currentRowDictionary = [[self tableData] objectAtIndex:row];

    NSString *voornaam = [currentRowDictionary objectForKey:@"voornaam"];
    NSString *achternaam = [currentRowDictionary objectForKey:@"achternaam"];
    NSString *tussenvoegsel = [currentRowDictionary objectForKey:@"tussenvoegsel"];

    voornaamLbl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 300, 40)] autorelease];
    voornaamLbl.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:19];
    voornaamLbl.text = voornaam;
    voornaamLbl.numberOfLines = 0;
    [voornaamLbl sizeToFit];

    tussenvoegselLbl = [[[UILabel alloc] initWithFrame:CGRectMake(voornaamLbl.frame.size.width + 10, 10, 300, 40)] autorelease];
    tussenvoegselLbl.text = tussenvoegsel;
    tussenvoegselLbl.numberOfLines = 0;
    [tussenvoegselLbl sizeToFit];

    static NSString *CellIdentifier = @"Cell";

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

    [cell.contentView addSubview:voornaamLbl];
    if ([tussenvoegsel length] != 0)
    {
        [cell.contentView addSubview:tussenvoegselLbl];
        achternaamLbl = [[[UILabel alloc] initWithFrame:CGRectMake(voornaamLbl.frame.size.width +
                                                                tussenvoegselLbl.frame.size.width + 15, 10, 300, 40)] autorelease];

    } else {
        achternaamLbl = [[[UILabel alloc] initWithFrame:CGRectMake(voornaamLbl.frame.size.width + 10, 10, 300, 40)] autorelease];
    }

    achternaamLbl.text = achternaam;
    achternaamLbl.numberOfLines = 0;
    [achternaamLbl sizeToFit];

    [cell.contentView addSubview:achternaamLbl];

    return cell;
}
4

1 に答える 1

0

セルが使用されるたびに、新しいラベルを割り当てて初期化します。セルはスクロール中に再利用されるため、毎回ラベルが乗算されます。

最良の方法は、ロード時にラベルを作成する別の UITableViewCell サブクラスを作成することです。その後、 cellForRowAtIndexPath で新しいセル サブクラスを使用し、ラベル テキストを設定します。

このチュートリアルの最後は、セルのサブクラスhttp://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1に役立ちます

于 2012-10-07T19:36:14.417 に答える