2012-10-07 59 views
1

当我滚动我的tableview时,文本从下面的单元格中获取所有内容。这也可能是因为我重新每次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; 
} 

回答

0

你的alloc和init新的标签,每次一个单元使用。滚动期间重复使用单元格,以便每次都乘以标签。

的最好方法是创建一个用于创建负载标签一个单独的UITableViewCell子类,然后在的cellForRowAtIndexPath你用你的新细胞子类,设置的标签文本

本教程的末端可以用细胞子http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1帮助

相关问题