相同在单元格中查看不同大小,如何重用单元格

时间:2013-03-04 09:43:54

标签: iphone ios uitableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.


    NSLog(@"[listArr count]=%d",[listArr count]);
    return [listArr count];



}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    PostCount *post=[listArr objectAtIndex:indexPath.row];


//     NSString *CellIdentifier = [NSString stringWithFormat: @"Cell_%d_%@",indexPath.row,post.foreignId];
//    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//        
//        NSLog(@"indexPath.row++++++++=%d",indexPath.row);
//        
//        
//    
//    }else{
////        [[cell.contentView viewWithTag:indexPath.row+1000] removeFromSuperview];
//       TimeLineGraphicView *gview=(TimeLineGraphicView *)[cell viewWithTag:indexPath.row+1000];
//         [gview removeFromSuperview];
//        gview=nil;
//    }
//   
//    
//    
//    TimeLineGraphicView *gview=[[TimeLineGraphicView alloc]init];
//    gview.tag=indexPath.row+1000;
//    gview.delegate=self;
//
//    [cell.contentView addSubview:gview];
//    
//    int Allheight =[ModelClass returnGraphicViewHeight_timeLine:post];
//    gview.frame=CGRectMake(0, 0, 320, Allheight);
//    [gview setViewStyle:post];
//    
//
//    gview.lab_time.text=[ModelClass intervalSinceNow:post.when btime:0];
//    
//    post=nil;
//    
//    return cell;

    static NSString *CellIdentifier = @"TimeLineGraphicViewCell";

    TimeLineGraphicViewCell *cell = (TimeLineGraphicViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){

        cell = [[TimeLineGraphicViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

    }


//    
//    int Allheight =[ModelClass returnGraphicViewHeight_timeLine:post];
//    cell.frame=CGRectMake(0, 0, 320, Allheight);


    //时间
    if (cell.btime==1) {
        cell.lab_time.text=[ModelClass intervalSinceNow:post.when btime:1 ];
    }else{
        cell.lab_time.text=[ModelClass intervalSinceNow:post.when btime:0];
    }


    cell.imageView_t.hidden=NO;
    //    imageView_t.isRoundIcon=YES;
    if(![GlobalVariable isBlankString:post.who.avatarbig])
    {

        [cell.imageView_t setImageWithURL:[NSURL URLWithString:post.who.avatarbig]
                    placeholderImage:[UIImage imageNamed:@"default_user.png"]];

    }
    else{
        cell.imageView_t.image=[UIImage imageNamed:@"default_user.png"];
    }


    cell.imageIcon.image=[UIImage imageNamed:[ModelClass returnimage_source:post.source]];
    cell.whoscreenName.text=post.who.screenName;


    CGSize size_myWhat =[ModelClass heightForTextView:[UIFont systemFontOfSize:15] WithText:post.what weith:250];


    cell.myWhat.frame=CGRectMake(50, 25, 250, size_myWhat.height);
    cell.myWhat.text=[NSString stringWithFormat:@"%@",post.what];
    cell.myWhaticon.frame=cell.myWhat.frame;


    if(![GlobalVariable isBlankString:post.img_small])
    {

        CGSize size_myImageView=CGSizeMake(80, 80);

        cell.myImageView.frame=CGRectMake(50, 30+size_myWhat.height, size_myImageView.width, 80);
        cell.myImageView.hidden=NO;


        [cell.myImageView setImageWithURL:[NSURL URLWithString:post.img_small]
                    placeholderImage:[UIImage imageNamed:@"default_user.png"]];


        UIButton * myImageBtn=[[UIButton alloc]init];
        myImageBtn.frame= CGRectMake(50, 30+size_myWhat.height, size_myImageView.width, 80);
        [myImageBtn addTarget:self action:@selector(my_imag_btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        myImageBtn.tag=self.tag-1000;
        [cell addSubview:myImageBtn ];


    }

    if ([post.sourceTweet.what length]>0) {
        cell.whoWhat.text=[NSString stringWithFormat:@"@%@: %@",post.sourceTweet.who.screenName,post.sourceTweet.what];

    }

    CGSize size_whoWhat =[ModelClass heightForTextView:[UIFont systemFontOfSize:14] WithText:cell.whoWhat.text weith:250];

    cell.whoWhat.frame=CGRectMake(10, 10, 250, size_whoWhat.height);

    if(![GlobalVariable isBlankString:post.sourceTweet.img_small])
    {



        CGSize size_whoImageView=CGSizeMake(80, 80);
        cell.whoImageView.frame=CGRectMake(30, 15+size_whoWhat.height, size_whoImageView.width, 80);
        cell.whoImageView.hidden=NO;
        [cell.whoImageView setImageWithURL:[NSURL URLWithString:post.sourceTweet.img_small]
                     placeholderImage:[UIImage imageNamed:nil]];


        UIButton * whoImageBtn=[[UIButton alloc]init];
        int myimageHeight=0;
        if (cell.myImageView.frame.size.height>0) {
            myimageHeight=5+80;
        }
        whoImageBtn.frame=CGRectMake(30+42, 40+size_myWhat.height+size_whoWhat.height+myimageHeight, 80, 80);
        whoImageBtn.backgroundColor=[UIColor clearColor];
        [whoImageBtn addTarget:self action:@selector(who_imag_btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        whoImageBtn.tag=self.tag-1000;
        [cell addSubview:whoImageBtn ];
    }



    if (size_whoWhat.height<20) {
        cell.whoBgView.hidden=YES;
    }else{

        int myimageHeight=0;
        if (cell.myImageView.frame.size.height>0) {
            myimageHeight=5+80;
        }

        cell.whoBgView.frame=CGRectMake(42, 25+size_myWhat.height+myimageHeight, 270, 20+size_whoWhat.height+cell.whoImageView.frame.size.height);

    }


    return cell;

}

和我的TimeLineGraphicViewCell

  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        imageView_t=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
        imageView_t.hidden=YES;


        imageView_t.layer.cornerRadius = 5;
        imageView_t.layer.masksToBounds = YES;
        //给图层添加一个有色边框
        imageView_t.layer.borderWidth = 0.8;
        imageView_t.layer.borderColor = [[UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1.0] CGColor];

        [self addSubview:imageView_t];


        imageIcon=[[UIImageView alloc]initWithFrame:CGRectMake(295, 5, 16, 16)];
        [self addSubview:imageIcon];



        self.lab_time = [[UILabel alloc] initWithFrame:CGRectMake(210, 5, 80, 20)];
        self.lab_time.textColor=[UIColor grayColor];
        self.lab_time.font=[UIFont systemFontOfSize:12];
        self.lab_time.textAlignment=UITextAlignmentRight;
        self.lab_time.backgroundColor=[UIColor clearColor];
        [self addSubview: self.lab_time];


        whoscreenName = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 145, 20)];
        whoscreenName.textColor=[UIColor blackColor];
        whoscreenName.font=[UIFont systemFontOfSize:18];
        whoscreenName.lineBreakMode = UILineBreakModeTailTruncation;
        whoscreenName.numberOfLines = 0;
        whoscreenName.backgroundColor=[UIColor clearColor];
        [self addSubview:whoscreenName];


        myWhat = [[UITextView alloc] init];
        myWhat.textColor=[UIColor blackColor];

        myWhat.backgroundColor=[UIColor clearColor];
        [myWhat setScrollEnabled:NO];
        [myWhat setEditable:NO];
        myWhat.dataDetectorTypes=UIDataDetectorTypeLink;
        [myWhat setCanCancelContentTouches:NO];
        [self addSubview:myWhat];

        myWhaticon=[[UIView alloc]init];
        [self addSubview:myWhaticon];


        myImageView=[[UIImageView alloc]init];
        myImageView.hidden=YES;
//         myImageView.compressed_img=1;
        [self addSubview:myImageView];




        whoBgView=[[UIImageView alloc]init];
        //        [whoBgView setImage:[[UIImage imageNamed:@"popup.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:30]];
        whoBgView.image = [[UIImage imageNamed:@"timeline_rt_border_t.png"] stretchableImageWithLeftCapWidth:130 topCapHeight:7];

        [self addSubview:whoBgView];


        whoWhat = [[UITextView alloc] init];
        whoWhat.textColor=[UIColor blackColor];

        whoWhat.backgroundColor=[UIColor clearColor];
        [whoWhat setScrollEnabled:NO];
        [whoWhat setEditable:NO];

        whoWhat.dataDetectorTypes=UIDataDetectorTypeLink;//url
        [whoWhat setCanCancelContentTouches:NO];
        [whoBgView addSubview:whoWhat];


        whoImageView=[[UIImageView alloc]init];
        whoImageView.hidden=YES;
        //         whoImageView.backgroundColor=[UIColor yellowColor];
//        whoImageView.compressed_img=1;
        [whoBgView addSubview:whoImageView];

        myWhat.font=[UIFont systemFontOfSize:15];
        whoWhat.font=[UIFont systemFontOfSize:14];
    }
    return self;
}

某些视图可能隐藏,而某些视图大小可能不同,我使用上面的代码,但是单元格位置错误。

1 个答案:

答案 0 :(得分:0)

TimeLineGraphicViewCell中创建方法,用给定数据计算它的高度。在ViewController创建临时单元格作为全局变量,并在heightForRowAtIndexPath中使用它来计算每个单元格的高度,使用它的给定数据。