自定义非等高cell02-xib
来源:互联网 发布:江苏省建筑节能数据库 编辑:程序博客网 时间:2024/06/06 07:09
在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
0 0
- 自定义非等高cell02-xib
- xib自定义非等高的cell
- 自定义非等高cell01-xib
- iOS开发总结之自定义等高cell02-storyboard(封装)
- iOS开发总结之自定义非等高cell-xib(方法一)
- 自定义等高的cell(使用XIB)
- tableView - 自定义等高cell-xib方式
- 自定义等高的cell(xib)
- iOS开发总结之自定义等高cell03-xib
- UI 一一 自定义等高cell (XIB方式)
- tableView-自定义非等高cell(1)
- tableView-自定义非等高cell(2)
- storyboard自定义非等高的Cell
- iOS 自定义cell高度(非等高)
- iOS开发总结之自定义非等高cell-storyboard
- UITableViewCell自定义等高
- UI基础_自定义非等高Cell_storyboard封装_版本_有GIF_源代码
- UI基础_自定义非等高Cell_Xib封装_版本_有GIF_源代码
- poj1465Multiple(经典BFS+余数判重)
- 引用类型——Java数组
- Android Studio 2.1 和 Unity3D 5.3.4 交互
- 28. Implement strStr()
- 121. Best Time to Buy and Sell Stock
- 自定义非等高cell02-xib
- Unity3D 摄像机(Camera)属性详解
- onvif的soap消息之getcapabilities
- iOS动画浅汇
- Java字符串编码和转换操作
- jQuery事件绑定和委托实例
- 给你的右键添加 to dos
- 树——二叉树的后序遍历(非递归)
- 面试题