在model中加入一个属性,cell的高度。
LMTestModel.h
#import <UIKit/UIKit.h> #import "LMTestModel.h" @interface LMTestCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *contentLabel; @property (weak, nonatomic) IBOutlet UIImageView *pictureView; +(LMTestCell *)cellWithTableView:(UITableView *)tableView; -(void)setContent:(NSUInteger)index cellModel:(LMTestModel *)model; @end
LMTestCell.h
#import <UIKit/UIKit.h> #import "LMTestModel.h" @interface LMTestCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *contentLabel; @property (weak, nonatomic) IBOutlet UIImageView *pictureView; +(LMTestCell *)cellWithTableView:(UITableView *)tableView; -(void)setContent:(NSUInteger)index cellModel:(LMTestModel *)model; @end
LMTestCell.m
#import "LMTestCell.h" #import "LMTestModel.h" @implementation LMTestCell +(LMTestCell *)cellWithTableView:(UITableView *)tableView { static NSString *cellid = @"LMTestCellID"; LMTestCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (!cell) { cell = [[NSBundle mainBundle]loadNibNamed:@"LMTestCell" owner:self options:nil][0]; } return cell; } -(void)awakeFromNib { [super awakeFromNib]; //设置label每一行文字的最大宽度 self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width-10*2; } -(void)setContent:(NSUInteger)index cellModel:(LMTestModel *)model{ [self.nameLabel setText:model.name]; [self.contentLabel setText:model.content]; if (model.url) { self.pictureView.hidden = NO; [self.pictureView setImage:[UIImage imageNamed:@"1"]]; NSURL *url = [NSURL URLWithString:model.url]; dispatch_async(dispatch_queue_create(0, DISPATCH_QUEUE_CONCURRENT), ^{ NSData *data = [NSData dataWithContentsOfURL:url]; dispatch_async(dispatch_get_main_queue(), ^{ [self.pictureView setImage:[UIImage imageWithData:data]]; }); }); }else { self.pictureView.hidden = YES; } [self layoutIfNeeded]; if (self.pictureView.hidden) { model.cellHeight = CGRectGetMaxY(self.contentLabel.frame)+10; }else { model.cellHeight = CGRectGetMaxY(self.pictureView.frame)+10; } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
ViewController.m
#import "ViewController.h" #import "LMTestCell.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> { UITableView *_weiboTableView; } /* 存放所有的model */ @property(nonatomic,strong)NSArray *models; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; _weiboTableView = tableView; _weiboTableView.delegate = self; _weiboTableView.dataSource = self; [self.view addSubview:_weiboTableView]; } -(NSArray *)models { if(!_models) { LMTestModel *model1 = [[LMTestModel alloc]init]; model1.name = @"丫丫乐"; model1.content = @"oh my baby baby ,oh...."; model1.url = @"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1462862903&di=4aedac5cd95d7605bc76d539064ddb14&src=http://img2.3lian.com/2014/f4/197/d/97.jpg"; LMTestModel *model2 = [[LMTestModel alloc]init]; model2.name = @"丫丫乐"; model2.content = @"新浪网新闻中心是新浪网最重要的频道之一,24小时滚动报道国内、国际及社会新闻。每日编发新闻数以万计。ews.sina.com.cn/ - 百度快照 - 77%好评"; _models = @[model1,model2]; } return _models; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.models.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LMTestCell *cell = [LMTestCell cellWithTableView:tableView]; LMTestModel *model = self.models[indexPath.row]; [cell setContent:indexPath.row cellModel:model]; return cell; } #pragma mark - 02-自定义非等高cell -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //cellForRowAtIndexPath:获取已经在tableivew中显示的cell。 LMTestModel *model = self.models[indexPath.row]; return model.cellHeight; } /** 返回每一行的估计高度 只返回了估计高度,那么就会先调用tableView:cellForRowIndexPath:方法创建cell,在调用tableView heightForRowAtIndexPath:方法获取cell的真实高度。 */ -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 250; } @end
时间: 2024-10-11 20:57:40