0

我正在将数据从 Web 服务传递到表格视图。Web 服务工作正常,但表格视图并非每次都显示数据。

在第一次或第二次之后,表格视图为空白。这是代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSLog(@"%@",appdelegate.LongeListArray);
    NSDictionary *dics = [appdelegate.LongeListArray objectAtIndex:indexPath.row];

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

        UIImageView *img=[[UIImageView alloc] init];
        img.image=[UIImage imageNamed:@"image_clipart.gif"];
        img.tag=1;
        [cell.contentView addSubview:img];

        UILabel *lblName=[[UILabel alloc] init];
        lblName.tag=2;
        lblName.backgroundColor=[UIColor clearColor];
        lblName.font=appdelegate.FontSmall;
        lblName.textAlignment=UITextAlignmentLeft;
        lblName.textColor=appdelegate.BlueColor;
        [cell.contentView addSubview:lblName];

        UIView *imgView=[[UIView alloc] init];
        imgView.tag=3;
        imgView.backgroundColor=[UIColor clearColor ];
        imgView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;

        [cell.contentView addSubview:imgView];
        UILabel *lblComments=[[UILabel alloc] init];
        lblComments.tag=4;
        lblComments.backgroundColor=[UIColor clearColor];
        lblComments.font=appdelegate.FontSmall;
        lblComments.textAlignment=UITextAlignmentLeft;
        lblComments.numberOfLines=4;
        lblComments.autoresizingMask=UIViewAutoresizingFlexibleWidth;
        lblComments.textColor=appdelegate.CaptionColor;
        [cell.contentView addSubview:lblComments];

        UILabel *lblDateTime=[[UILabel alloc] init];
        lblDateTime.tag=5;
        lblDateTime.backgroundColor=[UIColor clearColor];
        lblDateTime.font=appdelegate.FontSmall;
       // lblDateTime.textAlignment=UITextAlignmentRight;
        lblDateTime.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
        lblDateTime.autoresizingMask=YES;
        lblDateTime.textColor=appdelegate.DataColor;
        [cell.contentView addSubview:lblDateTime];

        if(appdelegate.isiPad)
        {
            img.frame=CGRectMake(10, 10, 75, 75);
            lblName.frame=CGRectMake(100, 5, 450, 25);
            lblComments.frame=CGRectMake(100, 25, 600, 100);
            lblDateTime.frame=CGRectMake(110, 80, 600, 100);
            imgView.frame=CGRectMake(125, 10, 160, 32);
        }
        else
        {
            img.frame=CGRectMake(10, 10, 50, 50);
            lblName.frame=CGRectMake(70, 5, 130, 25);
            lblComments.frame=CGRectMake(70, 25, 240, 100);
            lblDateTime.frame=CGRectMake(70, 80, 240, 100);
            imgView.frame=CGRectMake(200, 10, 110, 25);
        }

        if(appdelegate.selectedLanugage == ArabicLanguage)
        {
            [appdelegate ChangeUIForArabic:cell.contentView];
            imgView.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
            img.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
        }
        [img release];
        [lblName release];
        [imgView release];
        [lblComments release];
        [lblDateTime release];
    }
    UILabel *lblName=(UILabel *)[cell viewWithTag:2];
    UILabel *lblComments=(UILabel *)[cell viewWithTag:4];
    UILabel *lblDateTime=(UILabel *)[cell viewWithTag:5];
    UIView *ratingView=(UIView*)[cell.contentView viewWithTag:3];

    int x=0;
    int rating=[[dics valueForKey:@"lounge_rating"] intValue];
    for(int i=0;i<rating;i++)
    {
        UIImageView *imgRating=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rating_orange.png"]];
        if(appdelegate.isiPad)
        {
            imgRating.frame=CGRectMake(x, 0, 25, 26);
            x+=27;
        }
        else 
        {
            imgRating.frame=CGRectMake(x, 0, 17, 17);
            x+=19;
        }

        imgRating.backgroundColor=[UIColor clearColor];
        [ratingView addSubview:imgRating];

    }
    for(int i=0;i<(5-rating);i++)
    {
        UIImageView *imgRating=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rating_grey.png"]];
        if(appdelegate.isiPad)
        {
            imgRating.frame=CGRectMake(x, 0, 25, 26);
            x+=27;
        }
        else 
        {
            imgRating.frame=CGRectMake(x, 0, 17, 17);
            x+=19;
        }
        imgRating.backgroundColor=[UIColor clearColor];
        imgRating.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
        [ratingView addSubview:imgRating];
        [imgRating release];
        imgRating = nil;

    }


    if(!SearchFlag)
    {
        lblName.text=[dics valueForKey:@"Name"];
        lblComments.text=[dics valueForKey:@"lounge_Title"];
        lblDateTime.text=[appdelegate getDateTimeServerFormatted:[dics valueForKey:@"Start_Date"]];
    }
    else 
    {
        lblName.text=[dics valueForKey:@"Name"];
        lblComments.text=[dics valueForKey:@"discussion_title"];
        lblDateTime.text=[appdelegate getDateTimeServerFormatted:[dics valueForKey:@"added_date"]];
    }

    UIImage *imgTemp =(UIImage*)[dics valueForKey:@"UserImage"];
    UIImageView *imgView=(UIImageView*)[cell.contentView viewWithTag:1];
    if(imgTemp)
        imgView.image=imgTemp;
    else 
        imgView.image=[UIImage imageNamed:@"image_clipart.gif"];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;    

    return cell;
}
4

0 回答 0