我给每行的cell添加一个uiimageview,
这个uiimageview使用sdwebimage加载网络上的图片。
现在想动态设置tableview每行的高度为对应加载图片的高度。
但是在获取cell里的uiimageview对象时报错。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView * img=(UIImageView * )[[tableView cellForRowAtIndexPath:indexPath].subviews objectAtIndex:0];
   return  img.frame.size.height;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }//图片的url地址
NSString * str=[[[dataArr objectAtIndex:indexPath.row] objectForKey:@"file"] objectForKey:@"key"];
    
    UIImageView * img1=[[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 192, 100)];
    [img1  setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:ImgUrl,str]]];
    [cell addSubview:img1];
    [img1 release];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.layer.borderColor=[UIColor blackColor].CGColor;
    cell.layer.borderWidth=2;
    return cell;
}

解决方案 »

  1.   

    如果你想动态地生成高度,你就必须在获取内容的时候就已经计算出每一个CELL的高度;你可以将生成的内容画在cell的contentView上,然后计算contentView的高度,然后放到一个数组里,这样在这个方法里- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,就可以通过indexPath去获取每一个cell的高度了。至于你为什么会出错,是你还没有搞清楚UITableView的各方法的调用顺序,tableView:cellForRowAtIndexPath 是在tableView:heightForRowAtIndexPath之后的,你还没有生出来,怎么可能先说话?所以出错了。你去把苹果的UITableView文档多研究一下,google一下别人的解说也行。SO。。没了。
      

  2.   


    这种做法,我考虑过,就是在生成每个CELL时,将对应的高度保存起来。
    然后在heightForRowAtIndexPath方法根据indexPath获取对应高度。有些疑问,既然cellForRowAtIndexPath是在heightForRowAtIndexPath之后,那么heightForRowAtIndexPath方法里使用数组应该是为空。我通过NSLog发现是先输出cellForRowAtIndexPath的内容,后面在输出heightForRowAtIndexPath内容的。
      

  3.   


    sorry,刚试了一下。
    heightForRowAtIndexPath确实是在cellForRowAtIndexPath之后。现在的问题是,通过SDWebImage的 setImageWithURL方法给imageview设置image时,获取不到image的高度。也就是 image1.image.size.height; 获取到的是0.00000
      

  4.   

    不会的,你再检查一下,heightForRowAtIndexPath会先打印的。
      

  5.   


    对的,上面打错字了。
    确实是heightForRowAtIndexPath在前面。现在的问题是,通过SDWebImage的 setImageWithURL方法给imageview设置image时,获取不到image的高度。刚看了一下源码,这个setImageWithURL方法是异步下载的,获取image高时,image对象还为nil,不知道有什么优雅的方法能解决。我给你发了私信,方便加个Q吗?
      

  6.   

    图片加载完毕之后:
    [tableview beginUpdates];
    [tableview endUpdates];
    这个时候,tableview会再次循环cell的高度,这个时候你就应该能返回正确的高度了。
      

  7.   

    原则上heightForRowAtIndexPath这个方法应该从数据源计算出高度,而与cell无关
      

  8.   

    有2个方法,你参考一下。
    1 SD这个库在每个图片下载完之后会有回调,你在回调中计数,当计数值和你的资源列表的count一致的时候,reloadData,reloadData这个方法为重新执行heightForRowAtIndexPath。
    2 使用同步的方法下载资源,下载完后,就相当于在本地读取图片了。那时候,就可以取到图片的高度了。
      

  9.   

      你直接把你的需要cell里显示的content,在cellForRowAtIndexPath之前,在heightForRowAtIndexPath里算出来。  
       类似: 数据数组array,在heightForRowAtIndexPath里,把array的每一行拿出来算高度。再在cellForRowAtIndexPath里去填充数据!
      

  10.   

    楼主,我最近也在研究这个动态改变cell高度的问题,请问你是怎么解决的