源码-020501-自定义非等高cell-storyboard
2017-02-26 20:07
197 查看
// // XMGStatusesViewController.m // 备课03-不等高的cell-非代码 #import "XMGStatusesViewController.h" #import "XMGStatus.h" #import "XMGStatusCell.h" @interface XMGStatusesViewController () @property (strong, nonatomic) NSArray *statuses; @end @implementation XMGStatusesViewController - (NSArray *)statuses { if (_statuses == nil) { // 加载plist中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMutableArray *statusArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { XMGStatus *status = [XMGStatus statusWithDict:dict]; [statusArray addObject:status]; } _statuses = statusArray; } return _statuses; } - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.statuses.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { XMGStatusCell *cell = [XMGStatusCell cellWithTableView:tableView]; cell.status = self.statuses[indexPath.row]; return cell; } #pragma mark - 代理方法 /** * 返回每一行的高度 */ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { XMGStatus *staus = self.statuses[indexPath.row]; return staus.cellHeight; } /** * 返回每一行的估计高度 * 只要返回了估计高度,那么就会先调用tableView:cellForRowAtIndexPath:方法创建cell, 再调用tableView:heightForRowAtIndexPath:方法获取cell的真实高度 */ - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 200; } @end
// // XMGStatus.h #import <UIKit/UIKit.h> @interface XMGStatus : 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; /** cell的高度 */ @property (assign, nonatomic) CGFloat cellHeight; + (instancetype)statusWithDict:(NSDictionary *)dict; @end
// // XMGStatusCell.m // 07-自定义非等高cell-xib #import "XMGStatusCell.h" #import "XMGStatus.h" @interface XMGStatusCell() @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 *pictureView; @end @implementation XMGStatusCell + (instancetype)cellWithTableView:(UITableView *)tableView { return [tableView dequeueReusableCellWithIdentifier:@"status"]; } - (void)awakeFromNib { // 设置label每一行文字的最大宽度 // 为了保证计算出来的数值 跟 真正显示出来的效果 一致 self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20; } - (void)setStatus:(XMGStatus *)status { _status = status; if (status.isVip) { self.nameLabel.textColor = [UIColor orangeColor]; self.vipView.hidden = NO; } else { self.nameLabel.textColor = [UIColor blackColor]; self.vipView.hidden = YES; } self.nameLabel.text = status.name; self.iconView.image = [UIImage imageNamed:status.icon]; if (status.picture) { self.pictureView.hidden = NO; self.pictureView.image = [UIImage imageNamed:status.picture]; } else { self.pictureView.hidden = YES; } self.contentLabel.text = status.text; // 强制布局 [self layoutIfNeeded]; // 计算cell的高度 if (self.pictureView.hidden) { // 没有配图 status.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10; } else { // 有配图 status.cellHeight = CGRectGetMaxY(self.pictureView.frame) + 10; } } @end
相关文章推荐
- 源码-0203-06-自定义非等高cell-xib
- UI基础_自定义非等高Cell_storyboard封装_版本_有GIF_源代码
- storyboard自定义非等高的Cell
- iOS开发总结之自定义非等高cell-storyboard
- 利用storyboard 自定义cell
- 源码推荐(12.10B):清除缓存,自定义Cell,多边形按钮
- (素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI
- (素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI
- Spread 之自定义对角线cellType源码: DiagonalCellType
- 自定义不等高cell—storyBoard或xib自定义不等高cell
- storyboard 自定义tableviewcell,cell中的图片 回避重用机制,按照segue跳转
- iOS 自定义cell高度(非等高)
- iOS菜鸟学习——storyboard中自定义cell
- iOS边练边学--自定义非等高的cell
- 自定义cell的时候出现的错误(用的nib而不是storyboard)invalid nib registered for identifier ((null)) - nib must contain
- 自定义不等高的cell-(storyboard)
- Spread 之自定义对角线cellType源码: DiagonalCellType
- (素材源码)猫猫学IOS(十六)UI之XIB自定义Cell实现团购UI
- 源码推荐(5.05):快速集成版本新特性介绍页面,可以横向展示文本内容的自定义cell,根据文本无限滚动