重叠表格单元格内容[关闭]

Posted

技术标签:

【中文标题】重叠表格单元格内容[关闭]【英文标题】:Overlapping Table cells-content [closed] 【发布时间】:2013-10-18 09:50:37 【问题描述】:

最近我发现了一个非常烦人的错误:

两个单元格的内容混合在一个单元格中 我有 16 个单元格数据在滚动时显示,以前存在的单元格数据与要显示的当前数据重叠

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    return 100;




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView


    // Return the number of sections.
    if (flag == NO) 
        return 10;
    


    return [ytdRate count];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


    // Return the number of rows in the section.
    return 1;


- (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];
    



    if (flag == NO) 

    b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];

    // NSString *stri = [title objectAtIndex:indexPath.section];
    b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
    b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab1.numberOfLines = 0;
    b1Lab1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab1];

    b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
    NSString *st = [NSString stringWithFormat:@"YTD %@%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
    b1Lab2.textAlignment=NSTextAlignmentCenter;
    b1Lab2.text=st;
    b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab2.numberOfLines = 0;
    b1Lab2.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

    NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
//    NSArray *testItems = [tem componentsSeparatedByString:@"%"];
//    
//    NSDecimalNumber *value = [testItems objectAtIndex:0];

    float tempr = [tem floatValue];
    if(tempr <0)
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_red_80x20.png"]];
    
    else
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_green_80x20.png"]];
    

    [cell addSubview:b1Lab2];

    b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
    NSString *st1 = [NSString stringWithFormat:@"Net Asset Value Per Share"];
    b1Lab3.text=st1;
    b1Lab3.font = [UIFont fontWithName:@"Arial" size:13];
    [cell addSubview:b1Lab3];

        b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
        b1Lab8.textAlignment = NSTextAlignmentRight;
        NSString *st10 = [NSString stringWithFormat:@"%@", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
        b1Lab8.text=st10;
        b1Lab8.font = [UIFont fontWithName:@"Arial" size:13];
        [cell addSubview:b1Lab8];

    b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];

    NSString *st11 = [NSString stringWithFormat:@"1 YR: %@%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
    b1Lab4.text=st11;
    b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab4.numberOfLines = 0;
    b1Lab4.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab4];

    b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];

    NSString *st12 = [NSString stringWithFormat:@"3 YR: %@%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
    b1Lab5.text=st12;
    b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab5.numberOfLines = 0;
    b1Lab5.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab5];

    b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];

    NSString *st13 = [NSString stringWithFormat:@"5 YR: %@%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
    b1Lab6.text=st13;
    b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab6.numberOfLines = 0;
    b1Lab6.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab6];

    cell.backgroundColor = [UIColor whiteColor];
    
    else if(flag == YES)
    
        b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];

        // NSString *stri = [title objectAtIndex:indexPath.section];
        b1Lab1.text=[companyName objectAtIndex:indexPath.section];
        b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab1.numberOfLines = 0;
        b1Lab1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab1];

        b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
        NSString *st = [NSString stringWithFormat:@"YTD %@%%",[ytdRate objectAtIndex:indexPath.section]];
        b1Lab2.textAlignment=NSTextAlignmentCenter;
        b1Lab2.text=st;
        b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab2.numberOfLines = 0;
        b1Lab2.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

        NSString *tem = [ytdRate objectAtIndex:indexPath.section];
        //    NSArray *testItems = [tem componentsSeparatedByString:@"%"];
        //
        //    NSDecimalNumber *value = [testItems objectAtIndex:0];

        float tempr = [tem floatValue];
        if(tempr <0)
            b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_red_80x20.png"]];
        
        else
            b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_green_80x20.png"]];
        

        [cell addSubview:b1Lab2];

        b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
        NSString *st1 = [NSString stringWithFormat:@"Net Asset Value Per Share"];
        b1Lab3.text=st1;
        b1Lab3.font = [UIFont fontWithName:@"Arial" size:13];
        [cell addSubview:b1Lab3];

        b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
        b1Lab8.textAlignment = NSTextAlignmentRight;
        NSString *st10 = [NSString stringWithFormat:@"%@", [netAsset objectAtIndex:indexPath.section]];
        b1Lab8.text=st10;
        b1Lab8.font = [UIFont fontWithName:@"Arial" size:13];
        [cell addSubview:b1Lab8];



        b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];

        NSString *st11 = [NSString stringWithFormat:@"1 YR: %@%%",[oneYearValue objectAtIndex:indexPath.section]];
        b1Lab4.text=st11;
        b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab4.numberOfLines = 0;
        b1Lab4.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab4];

        b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];

        NSString *st12 = [NSString stringWithFormat:@"3 YR: %@%%",[threeYearValue objectAtIndex:indexPath.section]];
        b1Lab5.text=st12;
        b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab5.numberOfLines = 0;
        b1Lab5.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab5];

        b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];

        NSString *st13 = [NSString stringWithFormat:@"5 YR: %@%%",[fiveYearValue objectAtIndex:indexPath.section]];
        b1Lab6.text=st13;
        b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab6.numberOfLines = 0;
        b1Lab6.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab6];

        cell.backgroundColor = [UIColor whiteColor];
    

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;

您知道这种行为是如何发生的吗?

【问题讨论】:

【参考方案1】:

试试这个..

- (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];


[[cell viewWithTag:111]removeFromSuperview];
[[cell viewWithTag:222]removeFromSuperview];
[[cell viewWithTag:333]removeFromSuperview];
[[cell viewWithTag:444]removeFromSuperview];
[[cell viewWithTag:555]removeFromSuperview];
[[cell viewWithTag:666]removeFromSuperview];
[[cell viewWithTag:777]removeFromSuperview];

if (flag == NO) 

    b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];

    // NSString *stri = [title objectAtIndex:indexPath.section];
    b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
    b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab1.numberOfLines = 0;
    b1Lab1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab1.tag = 111;
    [cell addSubview:b1Lab1];

    b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
    NSString *st = [NSString stringWithFormat:@"YTD %@%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
    b1Lab2.textAlignment=NSTextAlignmentCenter;
    b1Lab2.text=st;
    b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab2.numberOfLines = 0;
    b1Lab2.tag = 222;
    b1Lab2.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

    NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
    //    NSArray *testItems = [tem componentsSeparatedByString:@"%"];
    //
    //    NSDecimalNumber *value = [testItems objectAtIndex:0];

    float tempr = [tem floatValue];
    if(tempr <0)
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_red_80x20.png"]];
    
    else
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_green_80x20.png"]];
    

    [cell addSubview:b1Lab2];

    b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
    NSString *st1 = [NSString stringWithFormat:@"Net Asset Value Per Share"];
    b1Lab3.text=st1;
    b1Lab3.font = [UIFont fontWithName:@"Arial" size:13];
    b1Lab2.tag=333;
    [cell addSubview:b1Lab3];

    b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
    b1Lab8.textAlignment = NSTextAlignmentRight;
    NSString *st10 = [NSString stringWithFormat:@"%@", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
    b1Lab8.text=st10;
    b1Lab8.font = [UIFont fontWithName:@"Arial" size:13];
    b1Lab2.tag=444;
    [cell addSubview:b1Lab8];

    b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];

    NSString *st11 = [NSString stringWithFormat:@"1 YR: %@%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
    b1Lab4.text=st11;
    b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab4.numberOfLines = 0;
    b1Lab4.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab2.tag=555;
    [cell addSubview:b1Lab4];

    b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];

    NSString *st12 = [NSString stringWithFormat:@"3 YR: %@%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
    b1Lab5.text=st12;
    b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab5.numberOfLines = 0;
    b1Lab5.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab2.tag=666;
    [cell addSubview:b1Lab5];

    b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];

    NSString *st13 = [NSString stringWithFormat:@"5 YR: %@%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
    b1Lab6.text=st13;
    b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab6.numberOfLines = 0;
    b1Lab6.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab2.tag=777;
    [cell addSubview:b1Lab6];

    cell.backgroundColor = [UIColor whiteColor];

else if(flag == YES)

    b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];

    // NSString *stri = [title objectAtIndex:indexPath.section];
    b1Lab1.text=[companyName objectAtIndex:indexPath.section];
    b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab1.numberOfLines = 0;
    b1Lab1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab1.tag = 111;
    [cell addSubview:b1Lab1];

    b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
    NSString *st = [NSString stringWithFormat:@"YTD %@%%",[ytdRate objectAtIndex:indexPath.section]];
    b1Lab2.textAlignment=NSTextAlignmentCenter;
    b1Lab2.text=st;
    b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab2.numberOfLines = 0;
    b1Lab2.tag = 222;
    b1Lab2.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

    NSString *tem = [ytdRate objectAtIndex:indexPath.section];
    //    NSArray *testItems = [tem componentsSeparatedByString:@"%"];
    //
    //    NSDecimalNumber *value = [testItems objectAtIndex:0];

    float tempr = [tem floatValue];
    if(tempr <0)
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_red_80x20.png"]];
    
    else
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_green_80x20.png"]];
    

    [cell addSubview:b1Lab2];

    b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
    NSString *st1 = [NSString stringWithFormat:@"Net Asset Value Per Share"];
    b1Lab3.text=st1;
    b1Lab3.font = [UIFont fontWithName:@"Arial" size:13];
    b1Lab3.tag = 333;
    [cell addSubview:b1Lab3];

    b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
    b1Lab8.textAlignment = NSTextAlignmentRight;
    NSString *st10 = [NSString stringWithFormat:@"%@", [netAsset objectAtIndex:indexPath.section]];
    b1Lab8.text=st10;
    b1Lab8.font = [UIFont fontWithName:@"Arial" size:13];
    b1Lab8.tag = 444;
    [cell addSubview:b1Lab8];



    b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];

    NSString *st11 = [NSString stringWithFormat:@"1 YR: %@%%",[oneYearValue objectAtIndex:indexPath.section]];
    b1Lab4.text=st11;
    b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab4.numberOfLines = 0;
    b1Lab4.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab4.tag = 555;
    [cell addSubview:b1Lab4];

    b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];

    NSString *st12 = [NSString stringWithFormat:@"3 YR: %@%%",[threeYearValue objectAtIndex:indexPath.section]];
    b1Lab5.text=st12;
    b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab5.numberOfLines = 0;
    b1Lab5.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab5.tag = 666;
    [cell addSubview:b1Lab5];

    b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];

    NSString *st13 = [NSString stringWithFormat:@"5 YR: %@%%",[fiveYearValue objectAtIndex:indexPath.section]];
    b1Lab6.text=st13;
    b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab6.numberOfLines = 0;
    b1Lab6.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    b1Lab6.tag = 777;
    [cell addSubview:b1Lab6];

    cell.backgroundColor = [UIColor whiteColor];


[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

return cell;

【讨论】:

for flag == NO 条件不工作 是的,但是对于 if(flag == NO) 条件它不起作用 当我只显示从服务器获取的正常数据时,它怎么会是我的问题,但是当我根据条件进行排序时,它会重叠(如果条件代码相同,你可以看到两者) aaray数据发送不同) 不,它无法评论该代码【参考方案2】:

tableView dequeueReusableCellWithIdentifier: 表示您正在请求特定类型(标识符)的 UITableViewCell 对象,根据是否已创建具有您的标识符的单元格,您将获得一个新的或旧的对象。

您的代码请求一组表格单元格并用 UILabel 填充它们。当您滚动时,您的表格视图会请求更多表格单元格。此时,由于已经创建了一些这种类型的单元格,您将获取旧对象 - 已在其中包含 UILabel 的旧表格单元格 - 然后在这些单元格中创建 更多 UILabel。这就是它们重叠的原因。

您可以停止使用可重复使用的单元格,而只需为每个表格单元格创建一个新的 UITableViewCell,但不建议这样做。随着表格单元数量的增加,这种方法将使用更多的内存,这是可重用单元旨在克服的问题。 this answer 有更详细的解释。

相反,您应该做的只是清除您检索到的可重用单元格中的所有内容,确保您的单元格在您再次创建 UILabel 时清除任何以前创建的内容。将以下代码添加到您的 cellForRowAtIndexPath (在您检索到可重用单元格之后)方法将起到作用:

for(UIView *view in [cell subviews])
    [view removeFromSuperview];

【讨论】:

for(UIView *view in [cell subviews]) [view removeFromSuperview];并为每个表格单元格创建一个新的 UITableViewCell 两个逻辑都不起作用【参考方案3】:
- (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];
  for (UIView *v [cell.contenetView subviews]
    [v removefromSuperView];

 ******* Change code  *********

    if (flag == NO) 

    b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];

    // NSString *stri = [title objectAtIndex:indexPath.section];
    b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
    b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab1.numberOfLines = 0;
    b1Lab1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab1];

    b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
    NSString *st = [NSString stringWithFormat:@"YTD %@%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
    b1Lab2.textAlignment=NSTextAlignmentCenter;
    b1Lab2.text=st;
    b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab2.numberOfLines = 0;
    b1Lab2.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

    NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
//    NSArray *testItems = [tem componentsSeparatedByString:@"%"];
//    
//    NSDecimalNumber *value = [testItems objectAtIndex:0];

    float tempr = [tem floatValue];
    if(tempr <0)
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_red_80x20.png"]];
    
    else
        b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_green_80x20.png"]];
    

    [cell addSubview:b1Lab2];

    b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
    NSString *st1 = [NSString stringWithFormat:@"Net Asset Value Per Share"];
    b1Lab3.text=st1;
    b1Lab3.font = [UIFont fontWithName:@"Arial" size:13];
    [cell addSubview:b1Lab3];

        b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
        b1Lab8.textAlignment = NSTextAlignmentRight;
        NSString *st10 = [NSString stringWithFormat:@"%@", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
        b1Lab8.text=st10;
        b1Lab8.font = [UIFont fontWithName:@"Arial" size:13];
        [cell addSubview:b1Lab8];

    b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];

    NSString *st11 = [NSString stringWithFormat:@"1 YR: %@%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
    b1Lab4.text=st11;
    b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab4.numberOfLines = 0;
    b1Lab4.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab4];

    b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];

    NSString *st12 = [NSString stringWithFormat:@"3 YR: %@%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
    b1Lab5.text=st12;
    b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab5.numberOfLines = 0;
    b1Lab5.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab5];

    b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];

    NSString *st13 = [NSString stringWithFormat:@"5 YR: %@%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
    b1Lab6.text=st13;
    b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
    b1Lab6.numberOfLines = 0;
    b1Lab6.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    [cell addSubview:b1Lab6];

    cell.backgroundColor = [UIColor whiteColor];
    
    else if(flag == YES)
    
        b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];

        // NSString *stri = [title objectAtIndex:indexPath.section];
        b1Lab1.text=[companyName objectAtIndex:indexPath.section];
        b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab1.numberOfLines = 0;
        b1Lab1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab1];

        b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
        NSString *st = [NSString stringWithFormat:@"YTD %@%%",[ytdRate objectAtIndex:indexPath.section]];
        b1Lab2.textAlignment=NSTextAlignmentCenter;
        b1Lab2.text=st;
        b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab2.numberOfLines = 0;
        b1Lab2.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];

        NSString *tem = [ytdRate objectAtIndex:indexPath.section];
        //    NSArray *testItems = [tem componentsSeparatedByString:@"%"];
        //
        //    NSDecimalNumber *value = [testItems objectAtIndex:0];

        float tempr = [tem floatValue];
        if(tempr <0)
            b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_red_80x20.png"]];
        
        else
            b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"val_green_80x20.png"]];
        

        [cell addSubview:b1Lab2];

        b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
        NSString *st1 = [NSString stringWithFormat:@"Net Asset Value Per Share"];
        b1Lab3.text=st1;
        b1Lab3.font = [UIFont fontWithName:@"Arial" size:13];
        [cell addSubview:b1Lab3];

        b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
        b1Lab8.textAlignment = NSTextAlignmentRight;
        NSString *st10 = [NSString stringWithFormat:@"%@", [netAsset objectAtIndex:indexPath.section]];
        b1Lab8.text=st10;
        b1Lab8.font = [UIFont fontWithName:@"Arial" size:13];
        [cell addSubview:b1Lab8];



        b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];

        NSString *st11 = [NSString stringWithFormat:@"1 YR: %@%%",[oneYearValue objectAtIndex:indexPath.section]];
        b1Lab4.text=st11;
        b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab4.numberOfLines = 0;
        b1Lab4.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab4];

        b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];

        NSString *st12 = [NSString stringWithFormat:@"3 YR: %@%%",[threeYearValue objectAtIndex:indexPath.section]];
        b1Lab5.text=st12;
        b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab5.numberOfLines = 0;
        b1Lab5.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab5];

        b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];

        NSString *st13 = [NSString stringWithFormat:@"5 YR: %@%%",[fiveYearValue objectAtIndex:indexPath.section]];
        b1Lab6.text=st13;
        b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
        b1Lab6.numberOfLines = 0;
        b1Lab6.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        [cell addSubview:b1Lab6];

        cell.backgroundColor = [UIColor whiteColor];
    

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;

新的更新答案

    static NSString *CellIdentifier = @"Cell";

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

【讨论】:

这对您有帮助吗? 我试过了,但问题没有解决 @naresh,我更新了我的代码。重试这个。在 for 循环中更改 v 而不是 vin 我已经改了但是还是不行 @naresh,将您的代码粘贴到 pastebin.com

以上是关于重叠表格单元格内容[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

UITableView“dequeueReusableCellWithIdentifier”导致基于单元格内容的动态单元格高度的单元格重叠?

Excel单元格内容包含指定单元格内容时,如何把“指定单元格2”复制到“单元格1”后面?

Excel如何合并多个单元格内容到一个单元格?

html表格里的单元格内容不能自动换行,求高手帮助解答!

表格视图高度之外的行的单元格内容发生变化(要查看此单元格,我们向下滚动)?

EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?