表视图分组表视图的问题

时间:2011-04-29 06:10:49

标签: iphone uitableview

tableview问题:我正在使用3 uilable来显示productname,some description和image。显示所有数据但滚动表格时标签上填充了另一个带有实际文本的文本..我们如何处理? 代码:

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

    }


UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 41, 41)];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    NSDictionary *aDict1   = [[NSDictionary alloc]init];
    aDict1 = [tableData objectAtIndex:indexPath.row];

    NSString *prdStus = [aDict1 objectForKey:@"ProductStatus"];
    NSLog(@"product status is %@ ",prdStus);
    if ([prdStus isEqualToString:@"Orange"]) {
        [imageview setImage:[UIImage imageNamed:@"Yellow.png"]];
    }
    if ([prdStus isEqualToString:@"Green"]) {
        [imageview setImage:[UIImage imageNamed:@"Green.png"]];
    }
    if ([prdStus isEqualToString:@"Red"]) {
        [imageview setImage:[UIImage imageNamed:@"Red.png"]];
    }

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(46, 0, 220, 12) ];
    label2.font = [UIFont systemFontOfSize:12];
    label2.text = @"";
    if (indexPath.row <[tableData count]) {
        label2.text = [aDict1 objectForKey:@"ProductName"];
    }

    [cell addSubview:label2];


    [cell addSubview:imageview];
    label2.backgroundColor =[UIColor clearColor];
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(46, 13, 220, 30) ];
    label3.font = [UIFont systemFontOfSize:10];
    label3.text = @"";
    label3.text = [aDict1 objectForKey:@"ProductDescription"];

    [cell addSubview:label3];


    return cell;

}

请告诉我如何避免这种情况..

分组表视图。

这也是我面临同样的问题。 我正在使用4节 第1和第3部分各1行, 第2秒3排, 第4秒5

配置文本时,第4和第3部分数据上显示的第一部分标签也显示在第4部分。 代码

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

    }

    cell.userInteractionEnabled =NO;

if (indexPath.section == 0) 

    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 2, 294, 40) ];
        label.backgroundColor = [UIColor clearColor];
        NSDictionary *aDict1   = [[NSDictionary alloc]init];
        aDict1 = [detailsArray objectAtIndex:0];

        label.text=@"";
        label.text =[aDict1 objectForKey:@"ProductName"];
        label.numberOfLines = 2;
        label.lineBreakMode = UILineBreakModeWordWrap;
        [cell addSubview:label];
        [label release];


    //  [cell addSubview:imgview1];
    //  [imgview1 release];

    }

    if (indexPath.section ==1 ) {

            if (indexPath.row == 0) 
        {
            imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 46, 46)];
            [cell addSubview:imgview];
            [imgview release];
            cell.textLabel.text = @"Actual Halal Rating";

            NSDictionary *aDict1   = [[NSDictionary alloc]init];
            aDict1 = [detailsArray objectAtIndex:0];

            NSString *statStr=[[NSString alloc] init];
            statStr = [aDict1 objectForKey:@"ProductHalalStatus"];
            NSLog(@"status is %@",statStr);

            imgview1 = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 20, 20)];


            if ([statStr isEqualToString:@"Red"]) {

                [imgview1 setImage:[UIImage imageNamed:@"Red.png"]];

            }
            if ([statStr isEqualToString:@"Orange"] ) {

                [imgview1 setImage:[UIImage imageNamed:@"Yellow.png"]];

            }
            if ([statStr isEqualToString:@"Green"]) {


                [imgview1 setImage:[UIImage imageNamed:@"Green.png"]];

            }
            [cell addSubview:imgview1];
            [imgview1 release];

        } 
        if (indexPath.row == 1) {
            cell.textLabel.text = @"Halal (Permisible)";
            [imgview setImage:[UIImage imageNamed:@"Green.png"]];
        }

        if (indexPath.row == 2) {
            cell.textLabel.text = @"Masbooh (Doubtful)";
            [imgview setImage:[UIImage imageNamed:@"Yellow.png"]];
        }

        if (indexPath.row == 3) {
            cell.textLabel.text = @"Haram (Not Permisible)";
            [imgview setImage:[UIImage imageNamed:@"Red.png"]];
        }

    } 


    if (indexPath.section == 2) {


        NSDictionary *aDict2   = [[NSDictionary alloc]init];
        aDict2 = [detailsArray objectAtIndex:0];
     // NSArray *ingrArr =[aDict2 objectForKey:@"IngredientInfo1"]; 
        textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)];
        //textview1.text = ingrStr;
        textview1.editable =NO;
        [textview1 setFont:[UIFont systemFontOfSize:15]];
        [cell addSubview:textview1];
        [textview1 release];

    }


    if (indexPath.section == 3) {


    }
    return cell;
}

1 个答案:

答案 0 :(得分:3)

从代码中删除该行....

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

并添加以下行

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]
相关问题