非等高cell,根据图片宽高,排布tableview

本文介绍了如何在UITableView中处理非等高cell,根据图片的宽高动态调整cell高度。当服务端返回图片尺寸时,直接计算高度;若不返回,则在图片加载完成后再刷新cell高度,确保布局正确。
摘要由CSDN通过智能技术生成

有2种情况:

1,服务端接口返回图片的宽高,这种比较简单,直接在

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

这个方法中根据图片宽高来返回cell的高度,简单处理下即可

2,服务端接口不返回图片的宽高,这种情况的话,一开始设置个cell的默认高度,在图片下载完成后刷新该行,重新设置cell的高度。具体代码如下:

开始是调用接口得到DetailModel的数组,设置DetailModel实例的imageUrl属性[self.tableView reloadData];

tableviewcontroller的部分实现:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    

    DetailModel *detailModel = [self.arr objectAtIndex:indexPath.row];

    

    if (detailModel.imageSize.width && detailModel.imageSize.height) {

        CGRect frame = cell.image.frame;

        frame.size.height325 * detailModel.imageSize.height / detailModel.imageSize.width;

        cell.image.frame = frame;

        

        [cell.image sd_setImageWithURL:[NSURL URLWithString:detailModel.imageUrl] placeholderImage:[UIImage imageNamed:@"loading"] ];

    } else {

        // 防止placeholder的图片拉伸

        CGRect frame = cell.image.frame;

        frame.size.height = 225;

        cell.image.frame = frame;

        

        [cell.image sd_setImageWithURL:[NSURL URLWithString:detailModel.imageUrl] placeholderImage:[UIImage imageNamed:@"loading"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

            

            NSLog(@"%@===%s===%@", [NSThread currentThread], __func__, NSStringFromCGSize(image.size));

            

            if (image && image.size.height && image.size.width) {

                detailModel.imageSize = image.size;

                CGRect frame = cell.image.frame;

                frame.size.height325 * image.size.height / image.size.width;

                cell.image.frame = frame;

                // 防止数组越界的崩溃,使用异步方法去刷新cell

                dispatch_async(dispatch_get_main_queue(), ^{

                    NSLog(@"%@ %@", [NSThread currentThread], indexPath);

                    [self.tableView reloadRowsAtIndexPaths:@[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) {

        return 325 * size.height / size.width + 20;

    } else {

        return 225 + 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 = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        //底部显示的大图片

        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 325, 225)];

        image.backgroundColor = [UIColor lightGrayColor];


        [self addSubview:image];

        self.image = image;

    }

    return self;

}

@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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值