在uitableview中滚动问题

时间:2011-09-13 07:55:34

标签: iphone

我有一个表格视图,我正在为uitableview单元格添加一些标签,实际屏幕如下enter image description here

当滚动2-3次时,它将显示如下enter image description here

enter image description here

,代码如下

和代码如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell;

    static NSString *CellIdentifier = @"Cell";      
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    switch (indexPath.section) {
            case 0:{
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

                NSString *drugName;
                if ([responsePrecDetails.drugName length]>0) {
                    NSString *startDrugName=[[responsePrecDetails.drugName substringToIndex:1] uppercaseString];
                    NSString *endDrugName=[[responsePrecDetails.drugName substringFromIndex:1] lowercaseString];
                    drugName=[NSString stringWithFormat:@"%@%@",startDrugName,endDrugName];
                }
                else {
                    drugName=@"NA";
                }


            UILabel *lblPrescName     = [self createCustomDynamicLabel:drugName
                                                               color:[UIColor walgreensRedColor]
                                                            fontsize:14 
                                                            fontName:@"HelveticaNeue-Bold" 
                                                        contentframe:CGRectMake(10,10,250 ,20)];
            [cell.contentView addSubview:lblPrescName];
            [lblPrescName release];
            break;
            }
        case 1:{

            if (indexPath.row==0 && [responsePrecDetails.status length]>0) {
                UILabel *lblStatus    = [self createCustomDynamicLabel:@"Status:"
                                                                color:[UIColor customisedblackColor]
                                                             fontsize:14 
                                                             fontName:@"HelveticaNeue-Bold" 
                                                         contentframe:CGRectMake(10,10,60 ,20)];
                [cell.contentView addSubview:lblStatus];
                [lblStatus release];
                UILabel *lblStatusAns = [self createCustomDynamicLabel:responsePrecDetails.status
                                                                 color:[UIColor customisedgreysColor]
                                                              fontsize:14 
                                                              fontName:@"HelveticaNeue" 
                                                          contentframe:CGRectMake(60,10,120,20)];
                [cell.contentView addSubview:lblStatusAns];
                [lblStatusAns release];

            }

            else {
                UILabel *lblDrName     = [self createCustomDynamicLabel:@"Dr "
                                                                 color:[UIColor customisedblackColor]
                                                              fontsize:14 
                                                              fontName:@"HelveticaNeue-Bold" 
                                                          contentframe:CGRectMake(10,7,25 ,20)];
                [cell.contentView addSubview:lblDrName];
                [lblDrName release];
                UILabel *lblDrNameAns  = [self createCustomDynamicLabel:[responsePrecDetails.rxer capitalizedString]
                                                                  color:[UIColor customisedgreysColor]
                                                               fontsize:14 
                                                               fontName:@"HelveticaNeue" 
                                                           contentframe:CGRectMake(30,7,150 ,20)];
                [cell.contentView addSubview:lblDrNameAns];
                [lblDrNameAns release];
                UILabel *lblName       = [self createCustomDynamicLabel:@"For: "
                                                               color:[UIColor customisedblackColor]
                                                            fontsize:14 
                                                            fontName:@"HelveticaNeue-Bold" 
                                                        contentframe:CGRectMake(10,26,40 ,20)];
                [cell.contentView addSubview:lblName];
                [lblName release];
                UILabel *lblNameAns    = [self createCustomDynamicLabel:[responsePrecDetails.patientName capitalizedString]
                                                                  color:[UIColor customisedgreysColor]
                                                               fontsize:14 
                                                               fontName:@"HelveticaNeue" 
                                                           contentframe:CGRectMake(40,26,120 ,20)];
                [cell.contentView addSubview:lblNameAns];
                [lblNameAns release];

                UILabel *lblDirections  = [self createCustomDynamicLabel:@"Directions For Use: "
                                                                  color:[UIColor customisedblackColor]
                                                               fontsize:14 
                                                               fontName:@"HelveticaNeue-Bold" 
                                                           contentframe:CGRectMake(10,44,230 ,20)];
                [cell.contentView addSubview:lblDirections];
                [lblDirections release];
                NSString *directionOfUse;
                NSLog(@"dirctionofuse=%@",responsePrecDetails.directionOfUse);
                if ([responsePrecDetails.directionOfUse length]>0) {
                    NSString *startDirectionOfUse=[[responsePrecDetails.directionOfUse substringToIndex:1] uppercaseString];
                    NSString *endDirectionOfUse=[[responsePrecDetails.directionOfUse substringFromIndex:1] lowercaseString];
                    directionOfUse=[NSString stringWithFormat:@"%@%@",startDirectionOfUse,endDirectionOfUse];
                }
                else {
                    directionOfUse=@"NA";
                }

                UILabel *lblDirectionsAns = [self createCustomDynamicLabel:directionOfUse
                                                                     color:[UIColor customisedgreysColor]
                                                                  fontsize:14 
                                                                  fontName:@"HelveticaNeue" 
                                                              contentframe:CGRectMake(150,35,284 ,35)];
                lblDirectionsAns.lineBreakMode  = UILineBreakModeWordWrap;
                lblDirectionsAns.numberOfLines  = 2;
                [cell.contentView addSubview:lblDirectionsAns];
                [lblDirectionsAns release];

                UILabel *lblRefills     = [self createCustomDynamicLabel:@"Refills:"
                                                                color:[UIColor customisedblackColor]
                                                             fontsize:14 
                                                             fontName:@"HelveticaNeue-Bold" 
                                                         contentframe:CGRectMake(10,92,50 ,20)];
                [cell.contentView addSubview:lblRefills];
                [lblRefills release];
                UILabel *lblRefillsAns=[self createCustomDynamicLabel:[responsePrecDetails.totalRfl stringByReplacingOccurrencesOfString:@"-" withString:@"/"]
                                                                color:[UIColor customisedgreysColor]
                                                             fontsize:14 
                                                             fontName:@"HelveticaNeue" 
                                                         contentframe:CGRectMake(60,92,150 ,20)];
                [cell.contentView addSubview:lblRefillsAns];
                [lblRefillsAns release];

                UILabel *lblPickupLocations=[self createCustomDynamicLabel:@"Pickup Location:"
                                                                     color:[UIColor customisedblackColor]
                                                                  fontsize:14 
                                                                  fontName:@"HelveticaNeue-Bold" 
                                                              contentframe:CGRectMake(10,110,150 ,20)];
                [cell.contentView addSubview:lblPickupLocations];
                [lblPickupLocations release];
                UILabel *lblPickupLocationsAns  = [self createCustomDynamicLabel:responsePrecDetails.pickupSore
                                                                          color:[UIColor customisedgreysColor]
                                                                       fontsize:14 
                                                                       fontName:@"HelveticaNeue" 
                                                                   contentframe:CGRectMake(10,130,250 ,43)];
                lblPickupLocationsAns.lineBreakMode = YES;
                lblPickupLocationsAns.numberOfLines = 2;
                [cell.contentView addSubview:lblPickupLocationsAns];
                [lblPickupLocationsAns release];

                UILabel *lblPhoneNo         = [self createCustomDynamicLabel:responsePrecDetails.phoneNo
                                                                 color:[UIColor customisedlightBlueColor]
                                                              fontsize:14 
                                                              fontName:@"HelveticaNeue-Bold" 
                                                          contentframe:CGRectMake(50,180,100 ,20)];
                [cell.contentView addSubview:lblPhoneNo];
                [lblPhoneNo release];
//              UILabel *lblDistance        = [self createCustomDynamicLabel:responsePrecDetails.distance
//                                                               color:[UIColor customisedlightBlueColor]
//                                                            fontsize:14 
//                                                            fontName:@"HelveticaNeue-Bold" 
//                                                        contentframe:CGRectMake(220,175,80 ,20)];
//              
//              [cell.contentView addSubview:lblDistance];
//              [lblDistance release];


//              UIImageView *miles  = [[UIImageView alloc] init];
//              miles.frame         = CGRectMake(180, 170, 30, 30);
//              miles.image         = [UIImage imageNamed:@"Images.bundle/Images/Flushot/miles_icon@2x.png"];
//              [cell.contentView addSubview:miles];
//              [miles release];
                UIImageView *phone  = [[UIImageView alloc] init];
                phone.frame         = CGRectMake(10, 177, 30, 30);
                phone.image         = [UIImage imageNamed:@"Images.bundle/Images/Flushot/phone_icon@2x.png"];
                [cell.contentView addSubview:phone];
                [phone release];
            }   
            break;

        default: {
            UILabel *cellLbl =[[UILabel alloc] init];
            cellLbl.frame=CGRectMake(0,0, 320, 55);

            orderInfo *prescInfo= [responsePrecDetails.order objectAtIndex:indexPath.section-2];
            UILabel *lblQty=[self createCustomDynamicLabel:@"Qty: "
                                                     color:[UIColor customisedgreysColor]
                                                  fontsize:14 
                                                  fontName:@"HelveticaNeue-Bold" 
                                              contentframe:CGRectMake(5,5,35 ,18)];
            [cellLbl addSubview:lblQty];
            [lblQty release];
            UILabel *lblPrice=[self createCustomDynamicLabel:@"Price: "
                                                       color:[UIColor customisedgreysColor]
                                                    fontsize:14 
                                                    fontName:@"HelveticaNeue-Bold" 
                                                contentframe:CGRectMake(5,23,50 ,18)];
            [cellLbl addSubview:lblPrice];
            [lblPrice release];

            UILabel *lblIns=[self createCustomDynamicLabel:@"Insurance: "
                                                     color:[UIColor customisedgreysColor]
                                                  fontsize:14 
                                                  fontName:@"HelveticaNeue-Bold"
                                              contentframe:CGRectMake(5,38,80 ,20)];
            [cellLbl addSubview:lblIns];
            [lblIns release];

            UILabel *lblQtyAns=[self createCustomDynamicLabel:prescInfo.orderQty
        color:[UIColor customisedgreysColor]
        fontsize:14 
        fontName:@"HelveticaNeue" 
        contentframe:CGRectMake(45,5,150 ,18)];
            [cellLbl addSubview:lblQtyAns];
            [lblQtyAns release];

            UILabel *lblPriceAns=[self createCustomDynamicLabel:[NSString stringWithFormat:@"$ %@",prescInfo.lastFlPrc]
                                                          color:[UIColor customisedgreysColor]
                                                       fontsize:14 
                                                       fontName:@"HelveticaNeue" 
                                                   contentframe:CGRectMake(55,23,150 ,18)];
            [cellLbl addSubview:lblPriceAns];
            [lblPriceAns release];

            UILabel *lblInsAns=[self createCustomDynamicLabel:prescInfo.ins
                                                        color:[UIColor customisedgreysColor]
                                                     fontsize:14 
                                                     fontName:@"HelveticaNeue" 
                                                 contentframe:CGRectMake(85,38,150 ,20)];
            [cellLbl addSubview:lblInsAns];
            [lblInsAns release];

            cell.backgroundView = cellLbl;
            [cellLbl release];

            break;
        }



        }
    }
    }
    return cell;


}

任何人都可以帮助我解决这个问题

2 个答案:

答案 0 :(得分:2)

release这个对象的原因是什么?

UILabel *lblStatus    = [self createCustomDynamicLabel:...];
[cell.contentView addSubview:lblStatus];
[lblStatus release];

如果您从createCustomDynamicLabel方法

收到自动释放的标签对象,请删除最后一行

答案 1 :(得分:0)

您需要在Switch案例中定义Cell Identifier。它重叠,因为所有单元格都使用单个标识符,而tableview从Cache中读取它们。