iOS UI-微博案例(通过代码自定义Cell)

一、Model

BWWeiBo数据模型

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface BWWeiBo : NSObject
 4 
 5 @property (nonatomic, copy) NSString *text;
 6 @property (nonatomic, copy) NSString *icon;
 7 @property (nonatomic, copy) NSString *picture;
 8 @property (nonatomic, copy) NSString *name;
 9 @property (nonatomic, assign, getter=isVip) BOOL vip;
10 
11 - (instancetype)initWithDict:(NSDictionary *)dict;
12 + (instancetype)weiBoWithDict:(NSDictionary *)dict;
13 
14 @end
15 
16 
17 #import "BWWeiBo.h"
18 
19 @implementation BWWeiBo
20 
21 - (instancetype)initWithDict:(NSDictionary *)dict
22 {
23     if (self = [super init]) {
24         [self setValuesForKeysWithDictionary:dict];
25     }
26     return self;
27 }
28 
29 + (instancetype)weiBoWithDict:(NSDictionary *)dict
30 {
31     return [[self alloc] initWithDict:dict];
32 }
33 
34 @end

BWWeiBoFrame的控件尺寸模型

  1 #import <Foundation/Foundation.h>
  2 #import <CoreGraphics/CoreGraphics.h>
  3 #import <UIKit/UIKit.h>
  4 @class BWWeiBo;
  5 
  6 @interface BWWeiBoFrame : NSObject
  7 
  8 @property (nonatomic, strong) BWWeiBo *weibo;
  9 
 10 @property (nonatomic, assign) CGRect iconFrame;
 11 @property (nonatomic, assign) CGRect nameFrame;
 12 @property (nonatomic, assign) CGRect vipFrame;
 13 @property (nonatomic, assign) CGRect textFrame;
 14 @property (nonatomic, assign) CGRect picFrame;
 15 
 16 @property (nonatomic, assign) CGFloat rowHeight;
 17 
 18 @end
 19 
 20 
 21 #import "BWWeiBoFrame.h"
 22 #import "BWWeiBo.h"
 23 
 24 #define nameFont [UIFont systemFontOfSize:12]
 25 #define textFont [UIFont systemFontOfSize:14]
 26 
 27 
 28 
 29 @implementation BWWeiBoFrame
 30 
 31 //重写weibo的set方法同时设置控件的frame
 32 - (void)setWeibo:(BWWeiBo *)weibo
 33 {
 34     _weibo = weibo;
 35     //提取统一的间距
 36     CGFloat margin = 10;
 37     //1.头像
 38     CGFloat iconW = 35;
 39     CGFloat iconH = 35;
 40     CGFloat iconX = margin;
 41     CGFloat iconY = margin;
 42     _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
 43     
 44     //2.昵称
 45     // 获取昵称字符串
 46     NSString *nickName = weibo.name;
 47     // 根据Label中文字的内容,动态计算Label的高和宽
 48     CGSize nameSize = [self sizeWithText:nickName andMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) andFont:nameFont];
 49     
 50     CGFloat nameX = CGRectGetMaxX(_iconFrame) +margin;
 51     CGFloat nameW = nameSize.width;
 52     CGFloat nameH = nameSize.height;
 53     CGFloat nameY = iconY + (iconH - nameH)/2;
 54     _nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
 55     
 56     //3.会员
 57     CGFloat vipW = 10;
 58     CGFloat vipH = 10;
 59     CGFloat vipX = CGRectGetMaxX(_nameFrame) + margin;
 60     CGFloat vipY = nameY;
 61     _vipFrame = CGRectMake(vipX, vipY, vipW, vipH);
 62     
 63     //4.正文
 64     CGFloat textX = iconX;
 65     CGFloat textY = CGRectGetMaxY(_iconFrame) +margin;
 66     CGSize textSize = [self sizeWithText:weibo.text andMaxSize:CGSizeMake(355, MAXFLOAT) andFont:textFont];
 67     CGFloat textW = textSize.width;
 68     CGFloat textH = textSize.height;
 69     _textFrame = CGRectMake(textX, textY, textW, textH);
 70     
 71     
 72     //5.配图
 73     CGFloat picW = 100;
 74     CGFloat picH = 100;
 75     CGFloat picX = iconX;
 76     CGFloat picY = CGRectGetMaxY(_textFrame) + margin;
 77     _picFrame = CGRectMake(picX, picY, picW, picH);
 78     
 79     //6.计算设置每行行高
 80     if (self.weibo.picture) {
 81         self.rowHeight = CGRectGetMaxY(_picFrame) + margin;
 82     }
 83     else
 84     {
 85         self.rowHeight = CGRectGetMaxY(_textFrame) +margin;
 86     }
 87 
 88 }
 89 
 90 //根据给定的字符串、最大值size、给定的字体、来计算文字应该占用的大小
 91 
 92 /**
 93  *  计算文字的尺寸
 94  *
 95  *  @param text    所要计算的文字
 96  *  @param maxSize 规定的文字尺寸范围,一般直限制宽度,而不限制高度
 97  *  @param font    计算文字时所用的字体"计算时用的字体大小,要和显示时的字体一样"
 98  *
 99  *  @return 计算出来的文字尺寸
100  */
101 - (CGSize) sizeWithText:(NSString *)text andMaxSize:(CGSize)maxSize andFont:(UIFont *)font
102 {
103     NSDictionary *attr = @{NSFontAttributeName : font};
104     return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
105 }
106 
107 @end

二、View

  1 #import <UIKit/UIKit.h>
  2 @class BWWeiBoFrame;
  3 
  4 @interface BWWeiBoCell : UITableViewCell
  5 
  6 @property (nonatomic, strong) BWWeiBoFrame *weiboFrame;
  7 
  8 + (instancetype)weiboCellWithTableView:(UITableView *)tableView;
  9 
 10 @end
 11 
 12 
 13 #import "BWWeiBoCell.h"
 14 #import "BWWeiBo.h"
 15 #import "BWWeiBoFrame.h"
 16 #define nameFont [UIFont systemFontOfSize:12]
 17 #define textFont [UIFont systemFontOfSize:14]
 18 
 19 
 20 @interface BWWeiBoCell ()
 21 
 22 @property (nonatomic, strong) UIImageView *imgViewIcon;
 23 @property (nonatomic, strong) UILabel     *lblNickName;
 24 @property (nonatomic, strong) UIImageView *imgViewVip;
 25 @property (nonatomic, strong) UILabel     *lblText;
 26 @property (nonatomic, strong) UIImageView *imgViewPicture;
 27 
 28 
 29 
 30 @end
 31 
 32 @implementation BWWeiBoCell
 33 
 34 #pragma mark - 重写单元格的initWithStyle:方法
 35 // 重写initWithStyle:方法在此方法中来创建自定义cell中所有要显示内容的子控件,在此方法中只创建和添加所有的子控件,并对子控件做一次的设置,不用设置子控件的数据和frame,因为此方法只会调用几次,当缓存池中有可重用的cell时,就不会调用此方法来创建cell了
 36 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 37 {
 38     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
 39         //创建控件
 40         
 41         //1.头像
 42         self.imgViewIcon = [[UIImageView alloc] init];
 43         [self.contentView addSubview:self.imgViewIcon];
 44         //2.昵称
 45         self.lblNickName = [[UILabel alloc] init];
 46         [self.contentView addSubview:self.lblNickName];
 47         //3.会员
 48         self.imgViewVip = [[UIImageView alloc] init];
 49         [self.contentView addSubview:self.imgViewVip];
 50         //4.正文
 51         self.lblText = [[UILabel alloc] init];
 52         [self.contentView addSubview:self.lblText];
 53         //5.配图
 54         self.imgViewPicture = [[UIImageView alloc] init];
 55         [self.contentView addSubview:self.imgViewPicture];
 56     }
 57     return self;
 58 }
 59 //创建单元格
 60 + (instancetype)weiboCellWithTableView:(UITableView *)tableView
 61 {
 62     static NSString *ID = @"weiBo_cell";
 63     
 64     BWWeiBoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
 65     if (!cell) {
 66         cell = [[BWWeiBoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
 67     }
 68     return cell;
 69 }
 70 #pragma mark - 设置数据和frame
 71 - (void)setWeiboFrame:(BWWeiBoFrame *)weiboFrame
 72 {
 73     _weiboFrame = weiboFrame;
 74     //1.设置当前单元格中子控件的数据
 75     [self settingData];
 76     //2.设置当前单元格中子控件的frame
 77     [self settingFrame];
 78 }
 79 
 80 
 81 //设置数据的方法
 82 - (void)settingData
 83 {
 84     BWWeiBo *weibo = self.weiboFrame.weibo;
 85     //1.头像
 86     self.imgViewIcon.image = [UIImage imageNamed:weibo.icon];
 87     //2.昵称
 88     self.lblNickName.text = weibo.name;
 89     self.lblNickName.font = nameFont;
 90     //3.会员
 91     if (weibo.isVip) {
 92         self.imgViewVip.image = [UIImage imageNamed:@"vip.png"];
 93         //显示会员图标
 94         self.imgViewVip.hidden = NO;
 95         //设置昵称的颜色
 96         self.lblNickName.textColor = [UIColor redColor];
 97     }
 98     else{
 99         //隐藏会员图标
100         self.imgViewVip.hidden = YES;
101         //设置昵称的颜色
102         self.lblNickName.textColor = [UIColor blackColor];
103     }
104     //4.正文
105     self.lblText.text = weibo.text;
106     self.lblText.font = textFont;
107     self.lblText.numberOfLines = 0;
108     //5.配图
109     if (weibo.picture) {
110         self.imgViewPicture.image = [UIImage imageNamed:weibo.picture];
111         self.imgViewPicture.hidden = NO;
112     }
113     else{
114         self.imgViewPicture.hidden = YES;
115     }
116 }
117 
118 //设置Frame
119 - (void)settingFrame
120 {
121     //1.头像
122     self.imgViewIcon.frame = self.weiboFrame.iconFrame;
123     
124     //2.昵称
125     self.lblNickName.frame = self.weiboFrame.nameFrame;
126     
127     //3.会员
128 
129     self.imgViewVip.frame = self.weiboFrame.vipFrame;
130     
131     //4.正文
132     self.lblText.frame = self.weiboFrame.textFrame;
133     
134     
135     //5.配图
136     self.imgViewPicture.frame = self.weiboFrame.picFrame;
137     
138     
139 }
140 
141 - (void)awakeFromNib {
142     // Initialization code
143 }
144 
145 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
146     [super setSelected:selected animated:animated];
147 
148     // Configure the view for the selected state
149 }
150 
151 @end

三、Controller

  1 #import "BWTableViewController.h"
  2 #import "BWWeiBo.h"
  3 #import "BWWeiBoFrame.h"
  4 #import "BWWeiBoCell.h"
  5 
  6 @interface BWTableViewController ()
  7 
  8 //现在要求weiboFrame集合中保存了很多个BWWeiBoFrame模型,不再是BWWeiBo模型
  9 @property (nonatomic, strong)NSArray *weiboFrames;
 10 
 11 @end
 12 
 13 @implementation BWTableViewController
 14 
 15 #pragma mark - 懒加载
 16 - (NSArray *)weiboFrames
 17 {
 18     if (!_weiboFrames) {
 19         NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
 20         NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];
 21         NSMutableArray *arrModel = [NSMutableArray array];
 22         
 23         for (NSDictionary *dict in arrDict) {
 24             //创建数据模型
 25             BWWeiBo *modelData = [BWWeiBo weiBoWithDict:dict];
 26             
 27             BWWeiBoFrame *modelFrame = [[BWWeiBoFrame alloc] init];
 28             
 29             modelFrame.weibo = modelData;
 30             
 31             [arrModel addObject:modelFrame];
 32         }
 33         _weiboFrames = arrModel;
 34     }
 35     return _weiboFrames;
 36 }
 37 
 38 
 39 - (void)viewDidLoad {
 40     [super viewDidLoad];
 41     //self.tableView.rowHeight = 400;
 42     
 43     // Uncomment the following line to preserve selection between presentations.
 44     // self.clearsSelectionOnViewWillAppear = NO;
 45     
 46     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 47     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 48 }
 49 
 50 #pragma mark - Table view data source
 51 
 52 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 53 
 54     return 1;
 55 }
 56 
 57 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 58     return [self.weiboFrames count];
 59 
 60 }
 61 
 62 //创建单元格
 63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 64     
 65     //1.获取数据模型
 66     BWWeiBoFrame *model = [self.weiboFrames objectAtIndex:indexPath.row];
 67     
 68     //2.创建单元格
 69     BWWeiBoCell *cell = [BWWeiBoCell weiboCellWithTableView:tableView];
 70     
 71     //3.设置单元格数据
 72     cell.weiboFrame = model;
 73     
 74     //4.返回单元格
 75     return cell;
 76 }
 77 
 78 
 79 #pragma mark - UITableViewDelegate
 80 //设置行高
 81 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 82 {
 83     BWWeiBoFrame *weiboFrame = self.weiboFrames[indexPath.row];
 84     return weiboFrame.rowHeight;
 85     
 86 }
 87 
 88 - (BOOL)prefersStatusBarHidden
 89 {
 90     return YES;
 91 }
 92 /*
 93 #pragma mark - Navigation
 94 
 95 // In a storyboard-based application, you will often want to do a little preparation before navigation
 96 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 97     // Get the new view controller using [segue destinationViewController].
 98     // Pass the selected object to the new view controller.
 99 }
100 */
101 
102 @end
原文地址:https://www.cnblogs.com/oc-bowen/p/5117118.html