-1

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

两个单元格的内容混合在一个单元格中 我有 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;
}

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

4

3 回答 3

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;}
于 2013-10-18T10:58:35.687 回答
0

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

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

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

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

for(UIView *view in [cell subviews])
    [view removeFromSuperview];
于 2013-10-18T10:03:58.193 回答
-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];
    }
于 2013-10-18T10:05:32.013 回答