我给每行的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;
}
这个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;
}
这种做法,我考虑过,就是在生成每个CELL时,将对应的高度保存起来。
然后在heightForRowAtIndexPath方法根据indexPath获取对应高度。有些疑问,既然cellForRowAtIndexPath是在heightForRowAtIndexPath之后,那么heightForRowAtIndexPath方法里使用数组应该是为空。我通过NSLog发现是先输出cellForRowAtIndexPath的内容,后面在输出heightForRowAtIndexPath内容的。
sorry,刚试了一下。
heightForRowAtIndexPath确实是在cellForRowAtIndexPath之后。现在的问题是,通过SDWebImage的 setImageWithURL方法给imageview设置image时,获取不到image的高度。也就是 image1.image.size.height; 获取到的是0.00000
对的,上面打错字了。
确实是heightForRowAtIndexPath在前面。现在的问题是,通过SDWebImage的 setImageWithURL方法给imageview设置image时,获取不到image的高度。刚看了一下源码,这个setImageWithURL方法是异步下载的,获取image高时,image对象还为nil,不知道有什么优雅的方法能解决。我给你发了私信,方便加个Q吗?
[tableview beginUpdates];
[tableview endUpdates];
这个时候,tableview会再次循环cell的高度,这个时候你就应该能返回正确的高度了。
1 SD这个库在每个图片下载完之后会有回调,你在回调中计数,当计数值和你的资源列表的count一致的时候,reloadData,reloadData这个方法为重新执行heightForRowAtIndexPath。
2 使用同步的方法下载资源,下载完后,就相当于在本地读取图片了。那时候,就可以取到图片的高度了。
类似: 数据数组array,在heightForRowAtIndexPath里,把array的每一行拿出来算高度。再在cellForRowAtIndexPath里去填充数据!