非等高cell,根据图片宽高,排布tableview
来源:互联网 发布:tinyumbrella java 编辑:程序博客网 时间:2024/06/05 05:44
有2种情况:
1,服务端接口返回图片的宽高,这种比较简单,直接在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
这个方法中根据图片宽高来返回cell的高度,简单处理下即可
2,服务端接口不返回图片的宽高,这种情况的话,一开始设置个cell的默认高度,在图片下载完成后刷新该行,重新设置cell的高度。具体代码如下:
开始是调用接口得到DetailModel的数组,设置DetailModel实例的imageUrl属性,[self.tableViewreloadData];
tableviewcontroller的部分实现:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DetailCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellID];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
DetailModel *detailModel = [self.arrobjectAtIndex:indexPath.row];
if (detailModel.imageSize.width && detailModel.imageSize.height) {
CGRect frame = cell.image.frame;
frame.size.height = 325 * detailModel.imageSize.height / detailModel.imageSize.width;
cell.image.frame = frame;
[cell.imagesd_setImageWithURL:[NSURLURLWithString:detailModel.imageUrl]placeholderImage:[UIImageimageNamed:@"loading"] ];
} else {
// 防止placeholder的图片拉伸
CGRect frame = cell.image.frame;
frame.size.height =225;
cell.image.frame = frame;
[cell.imagesd_setImageWithURL:[NSURLURLWithString:detailModel.imageUrl]placeholderImage:[UIImageimageNamed:@"loading"]completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType,NSURL *imageURL) {
NSLog(@"%@===%s===%@", [NSThreadcurrentThread], __func__,NSStringFromCGSize(image.size));
if (image && image.size.height && image.size.width) {
detailModel.imageSize = image.size;
CGRect frame = cell.image.frame;
frame.size.height = 325 * image.size.height / image.size.width;
cell.image.frame = frame;
// 防止数组越界的崩溃,使用异步方法去刷新cell
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@ %@", [NSThreadcurrentThread], indexPath);
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
});
}
}];
}
return cell;
}
#pragma mark- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
DetailModel *detailModel =self.arr[indexPath.row];
CGSize size = detailModel.imageSize;
if (size.width && size.height) {
return325 * size.height / size.width +20;
} else {
return225 + 20;
}
}
DetailCell的实现:
DetailCell.h
#import <UIKit/UIKit.h>
@interface DetailCell : UITableViewCell
/** 底部显示的大图 */
@property (nonatomic,weak) UIImageView *image;
@end
DetailCell.m
#import "DetailCell.h"
@implementation DetailCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//底部显示的大图片
UIImageView *image = [[UIImageViewalloc] initWithFrame:CGRectMake(10,10, 325,225)];
image.backgroundColor = [UIColorlightGrayColor];
[selfaddSubview:image];
self.image = image;
}
returnself;
}
@end
DetailModel的实现:
DetailModel.h
#import <UIKit/UIKit.h>
@interface DetailModel : NSObject
@property (nonatomic,copy) NSString *imageUrl;
@property (nonatomic,assign) CGSize imageSize;
@end
DetailModel.m
#import "DetailModel.h"
@implementation DetailModel
@end
- 非等高cell,根据图片宽高,排布tableview
- tableView-自定义非等高cell(1)
- tableView-自定义非等高cell(2)
- tableview非等高的cell
- tableView - 自定义等高cell-storyBoard方式
- tableView - 自定义等高cell-xib方式
- xib自定义非等高的cell
- storyboard自定义非等高的Cell
- iOS 自定义cell高度(非等高)
- tableView - 自定义等高cell(frame和masonry方式)
- tableview cell自适应撑高
- iOS开发总结之自定义非等高cell-storyboard
- tableView中cell、collectionView中cell选中后,图片加上一层高亮效果 selected highlighted
- 自定义等高的cell
- iOS 自定义等高cell
- 自定义等高的cell
- qt tableview 根据内容设置行高
- iOS开发总结之自定义非等高cell-xib(方法一)
- 初识Spring-MVC之最小配置运行Hello world的maven项目
- 移动APP调试最佳实践二
- 最新ps cs6序列号 永久免费可用
- c3p0--Jdbc连接池的基本使用
- Bootstrap嵌入jqGrid,使你的table牛逼起来
- 非等高cell,根据图片宽高,排布tableview
- Bayer RGB和RGB Raw
- iOS开发之自定义AlertView
- 【AS/400】如何查找到含指定字符串的代码文件
- awk-11
- 浏览器的多进程架构
- Android JsonObject 数据的遍历
- 将几个PDF合并为一个文件的方法
- java 调用webservice的各种方法总结