重叠表格细胞 - 内容

时间:2013-10-18 09:50:37

标签: iphone uitableview ios7

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

两个细胞的含量在一个细胞中混合 滚动显示有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;
}

您是否知道这种行为会如何发生?

3 个答案:

答案 0 :(得分: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];
}

[[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;}

答案 1 :(得分:0)

tableView dequeueReusableCellWithIdentifier:表示您正在请求特定类型(标识符)的UITableViewCell对象,并且您将获得新的或旧的对象,具体取决于是否已创建具有您的标识符的单元格

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

您可以停止使用可重复使用的单元格,只需为每个表格单元格创建一个新的UITableViewCell,但不建议这样做。随着表格单元数量的增加,该方法将使用更多的存储器,这是可重用单元旨在克服的问题。 this answer中提供了更详细的说明。

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

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

答案 2 :(得分:-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];}
  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];
    }
相关问题