[iOS微博项目

A.会员标识
1.需求
给vip会员打上会员标识
不同等级的vip会员使用不同的标识
使用橙色作为昵称颜色
 
Image(157)
 
2.思路
返回的user数据中有两个字段
mbrank:int 会员等级
mbtype:int 会员类型,大于2才是会员
 
3.实现
依照之前的做法,在微博内容界面加上一个ImageView作为vip标识
创建相应的frame
 
(1)在user模型加上vip相关字段
 1 //
 2 //  HVWUser.h
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/5.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface HVWUser : NSObject
12 
13 /** 友好显示名称 */
14 @property(nonatomic, copy) NSString *name;
15 
16 /** 用户头像地址(中图),50×50像素 */
17 @property(nonatomic, copy) NSString *profile_image_url;
18 
19 /** 用户昵称 */
20 @property(nonatomic, copy) NSString *screen_name;
21 
22 /** 会员等级 */
23 @property(nonatomic, assign) int mbrank;
24 
25 /** 会员类型 */
26 @property(nonatomic, assign) int mbtype;
27 
28 /** 是否是会员 */
29 @property(nonatomic, assign, getter=isVip) BOOL vip;
30 
31 @end
 
 
 1 //
 2 //  HVWUser.m
 3 //  HVWWeibo
 4 //
 5 //  Created by hellovoidworld on 15/2/5.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "HVWUser.h"
10 
11 @implementation HVWUser
12 
13 /** 判断是否是会员 */
14 - (BOOL)isVip {
15     return self.mbtype > 2;
16 }
17 
18 @end
19  
202)加上vip标识的view
21 //  HVWStatusOriginalView.m
22 /** vip会员标识 */
23 @property(nonatomic, weak) UIImageView *vipView;
24  
25 /** 代码初始化方法 */
26 - (instancetype)initWithFrame:(CGRect)frame {
27     self = [super initWithFrame:frame];
28    
29     if (self) { // 初始化子控件开始
30 ...
31        
32         // vip会员
33         UIImageView *vipView = [[UIImageView alloc] init];
34         vipView.contentMode = UIViewContentModeCenter;
35         self.vipView = vipView;
36         [self addSubview:vipView];
37        
38 ...
39     }
40    
41     return self;
42 }
43  
44 /** 设置frame */
45 - (void)setOriginalFrame:(HVWStatusOriginalFrame *)originalFrame {
46 ...
47    
48     // vip会员标识
49     if (user.isVip) {
50         self.nameLabel.textColor = [UIColor orangeColor];
51         self.vipView.hidden = NO;
52         self.vipView.frame = originalFrame.vipFrame;
53         self.vipView.image = [UIImage imageWithNamed:[NSString stringWithFormat:@"common_icon_membership_level%d", user.mbrank]];
54        
55     } else { // 注意cell的重用问题,需要回复设置
56         self.nameLabel.textColor = [UIColor blackColor];
57         self.vipView.hidden = YES;
58     }
59   
60 ...
61 }
 
(3)计算frame
 1 //  HVWStatusOriginalFrame.m
 2 /** 加载微博数据 */
 3 - (void)setStatus:(HVWStatus *)status {
 4     _status = status;
 5    
 6 ...
 7    
 8     // vip会员标识
 9     if (user.isVip) {
10         CGFloat vipX = CGRectGetMaxX(self.nameFrame) + HVWStatusCellInset;
11         CGFloat vipY = nameY;
12         CGFloat vipWidth = nameSize.height;
13         CGFloat vipHeight = vipWidth;
14         self.vipFrame = CGRectMake(vipX, vipY, vipWidth, vipHeight);
15     }
16 ...
17 }
 
原文地址:https://www.cnblogs.com/hellovoidworld/p/4311078.html