iOS开发总结之自定义非等高cell-storyboard

来源:互联网 发布:软件需求工程师转行 编辑:程序博客网 时间:2024/05/23 11:36

1.storyboard中的结构:


2.代码


#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

#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

#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

3.效果:


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕妇被小猫抓了怎么办 怀孕了家里有猫怎么办 厕所被湿纸巾堵了怎么办 5天新生儿不拉屎怎么办 4月宝宝不拉屎怎么办 两岁宝宝晚上睡觉哭闹怎么办 2月婴儿吐奶很多怎么办 心情不好回奶了怎么办 四个月了没奶怎么办 八岁宝宝还尿床怎么办 自己一人在家害怕怎么办 被猫抓伤流血了怎么办 被小狐狸咬了怎么办 怀孕吃了兔子肉怎么办 鸟总在窗子上啄怎么办? 有鸟飞进楼道里怎么办 租的房间房东要求改建怎么办 小偷偷钱抓住不还钱怎么办 损友圈羊被陌生人偷了怎么办 在酒店如果遇到客人偷东西怎么办 梦见家里有不好的东西怎么办 被小孩要破了怎么办啊 租一个房子小孩一进房就哭怎么办 墙缝里有蝙蝠窝怎么办 小蝙蝠在墙缝里怎么办 小孩一进屋就哭怎么办 屋门对着厕所门怎么办 入室门对厨房门怎么办 厕所正对入户门怎么办 小区楼交错冲路怎么办 床的位置在五鬼上怎么办 被甩了很痛苦怎么办 和对象想分手了怎么办 对象想跟你啪啪怎么办 相亲对象好像不太想理我怎么办 想跟对象分手了怎么办 异地恋分手后该怎么办 面膜敷了一晚上怎么办 梦见被刺猬咬了怎么办 梦见死人叫我名字答应怎么办 香瓜苗叶子长斑怎么办