xib自定义非等高的cell
来源:互联网 发布:linux 局域网传文件 编辑:程序博客网 时间:2024/06/06 03:51
/* 1.在模型中增加一个 cellHeight属性,用来存放对于cell的高度 2.在cell的模型属性的set方法中调用[self layoutIfNeed]方法强制布局,然后计算出cellHeight的值 3.控制器中实现tableView:estimatedHeightForRowAtIndexPath方法,返回一个cell的估计高度,比如200 4.控制器中实现tableView:heightForRowAtIndexPath方法,返回cell的真实高度(模型中的cellHeight属性) */
//// WeiboCell.h#import <UIKit/UIKit.h>@class WeiboModel;@interface WeiboCell : UITableViewCell+ (instancetype)cellWithTableView:(UITableView *)tableView;/** * 模型属性 */@property(nonatomic,strong)WeiboModel *model;@end
//// WeiboCell.m#import "WeiboCell.h"#import "WeiboModel.h"@interface WeiboCell()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;@property (weak, nonatomic) IBOutlet UIImageView *vipView;@property (weak, nonatomic) IBOutlet UILabel *contentLabel;@property (weak, nonatomic) IBOutlet UIImageView *picView;@end@implementation WeiboCell+ (instancetype)cellWithTableView:(UITableView *)tableView{ static NSString *ID = @"weibo"; WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } return cell;}- (void)awakeFromNib{ //设置label每一行文字的最大宽度 //让UILabel计算尺寸更加准确 self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;;}- (void)setModel:(WeiboModel *)model{ _model = model; if (model.isVip) { self.nameLabel.textColor = [UIColor orangeColor]; self.vipView.hidden = NO; }else{ self.nameLabel.textColor = [UIColor blackColor]; self.vipView.hidden = YES; } self.iconView.image = [UIImage imageNamed:model.icon]; self.nameLabel.text = model.name; self.contentLabel.text = model.text; if (model.picture) { self.picView.hidden = NO; self.picView.image = [UIImage imageNamed:model.picture]; }else{ self.picView.hidden = YES; } //强制布局 [self layoutIfNeeded]; //计算cell的高度 if (self.picView.hidden) { //没有配图 model.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10; }else{ //有配图 model.cellHeight = CGRectGetMaxY(self.picView.frame) + 10; }}@end模型:
//// WeiboModel.h#import <UIKit/UIKit.h>@interface WeiboModel : NSObject@property (strong, nonatomic) NSString *name;@property (strong, nonatomic) NSString *text;@property (strong, nonatomic) NSString *icon;@property (strong, nonatomic) NSString *picture;@property (assign, nonatomic, getter=isVip) BOOL vip;+ (instancetype)weiboWithDict:(NSDictionary *)dict;/** * cell的高度 */@property(nonatomic,assign)CGFloat cellHeight;@end
//// WeiboModel.m#import "WeiboModel.h"@implementation WeiboModel+ (instancetype)weiboWithDict:(NSDictionary *)dict{ WeiboModel *model = [[self alloc] init]; [model setValuesForKeysWithDictionary:dict]; return model;}@end
控制器里:
//// WeiboController.m#import "WeiboController.h"#import "WeiboModel.h"#import "WeiboCell.h"@interface WeiboController ()@property (strong, nonatomic) NSArray *weibos;@end@implementation WeiboController- (NSArray *)weibos{ if (_weibos == nil) { // 加载plist中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMutableArray *ArrayM = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { WeiboModel *model = [WeiboModel weiboWithDict:dict]; [ArrayM addObject:model]; } _weibos = ArrayM; } return _weibos;}- (void)viewDidLoad { [super viewDidLoad];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.weibos.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ WeiboCell *cell = [WeiboCell cellWithTableView:tableView]; cell.model = self.weibos[indexPath.row]; return cell;}#pragma mark - 代理方法/** * 返回每一行的高度 */- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ WeiboModel *model = self.weibos[indexPath.row]; return model.cellHeight;}/** * 估计cell的高度 * 只要返回了估计高度,那么就会先调用tableView:cellForRowAtIndexPath:创建cell, * 再调用tableView:heightForRowAtIndexPath:返回cell的真实高度 */- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 200;}@end最后效果:
0 0
- xib自定义非等高的cell
- 自定义等高的cell(使用XIB)
- 自定义等高的cell(xib)
- storyboard自定义非等高的Cell
- tableView - 自定义等高cell-xib方式
- iOS开发总结之自定义非等高cell-xib(方法一)
- 自定义非等高cell01-xib
- 自定义非等高cell02-xib
- tableView-自定义非等高cell(1)
- tableView-自定义非等高cell(2)
- iOS 自定义cell高度(非等高)
- UI 一一 自定义等高cell (XIB方式)
- 自定义等高的cell
- 自定义等高的cell
- tableview非等高的cell
- iOS开发总结之自定义非等高cell-storyboard
- 自定义等高的cell(纯代码)
- 自定义等高的cell(使用stroyboard)
- jQuery.extend函数详解--
- poj2001 Shortest Prefixes (trie树)
- 并查集
- AIM Tech Round (Div. 2) D. Array GCD(dp)
- POJ3694 Network(边双连通+LCA)
- xib自定义非等高的cell
- 数据结构基础之双向链表(约瑟夫问题)
- 简易图解移轴镜头 (Tilt-Shift Lens) 原理 简易图解移轴镜头 (Tilt-Shift Lens) 原理
- Codeforces 577A Multiplication Table
- 界面元查询参数构件
- 穷人的“移轴”,普通镜头秒变“移轴”镜头 - 新增倾斜实拍
- 安卓开发——invalidate()自动清屏和屏幕刷新
- 文本换行word-wrap、word-break与white-space
- bzoj2120 数颜色 分块