iOS实践04
第四天
微博數據展示:獲取服務器數據,json數據的解析,MVC的使用,自定義cell高度的計算,一些分類的設計。已經是第四天了,雖然每天都有課程,但這個東西也基本完成了一大半吧,一些忘掉的知識也撿起來了。看了一下第一次寫的筆記,做到這個程度可是花了將近十多天的時間,這次這么短的時間就做到了著,還是有一點復制粘貼的成分,但是大致的思維過程還是有的,也練了一下編碼速度的。四天時間主要就是回顧一下之前做的一些事情,下一步不打算接著做下去了,要將第一次沒完成的很多細節完善,爭取做得更加完美。簡單的記錄下今天吧,很多課上的很沒意思的說。
1.很明顯需要自定義cell,而微博展示這種比較復雜的cell,我們在一開始就把所有要用到的控件加進去,然后再根據數據選擇是否進行顯示。
2.首先是要在home模塊中向新浪請求數據,[self loadStatus],方法中我們需要使用賬號的access_token作為請求參數,通過SVAccountTool工具類很容易得到。
1 - (void)loadStatus 2 { 3 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 4 5 NSMutableDictionary *pramas = [NSMutableDictionary dictionary]; 6 // 拿到當前賬號 7 SVAccount *account = [SVAccountTool account]; 8 pramas[@"access_token"] = account.access_token; 9 10 [manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:pramas progress:^(NSProgress * _Nonnull uploadProgress) { 11 12 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 13 // 將字典數組轉為模型數組(里面放的就是IWStatus模型) 14 NSArray *statusArray = [SVStatus mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]]; 15 // 創建frame模型對象 16 NSMutableArray *statusFrameArray = [NSMutableArray array]; 17 for (SVStatus *status in statusArray) { 18 SVStatusFrame *statusFrame = [[SVStatusFrame alloc] init]; 19 // 傳遞微博模型數據 20 statusFrame.status = status; 21 [statusFrameArray addObject:statusFrame]; 22 } 23 // 賦值 24 self.statusFrames = statusFrameArray; 25 // 刷新表格 26 [self.tableView reloadData]; 27 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 28 NSLog(@"status error--%@", error); 29 }]; 30 }?
3.在請求數據后,服務器會返回json數據,為了面向模型開發,添加SVStatus模型和SVUser模型,并根據新浪返回數據和我們需要的數據為其添加成員變量。(在這里第一次的時候是先進行了數據解析,直接在系統的cell上展示了頭像、用戶名等數據,這次做就直接自定義cell)
1 #import <Foundation/Foundation.h> 2 @class SVUser; 3 @interface SVStatus : NSObject 4 /** 5 * 微博創建時間 6 */ 7 @property (nonatomic, copy) NSString *created_at; 8 /** 9 * 微博ID 10 */ 11 @property (nonatomic, copy) NSString *ID; 12 /** 13 * 微博內容 14 */ 15 @property (nonatomic, copy) NSString *text; 16 /** 17 * 微博來源 18 */ 19 @property (nonatomic, copy) NSString *source; 20 /** 21 * 微博配圖的縮略圖地址 22 */ 23 @property (nonatomic, copy) NSString *thumbnail_pic; 24 /** 25 * 轉發數 26 */ 27 @property (nonatomic, assign) int retwweted_count; 28 /** 29 * 評論數 30 */ 31 @property (nonatomic, assign) int reposts_count; 32 /** 33 * 點贊數 34 */ 35 @property (nonatomic, assign) int attitudes_count; 36 /** 37 * 發微博的人 38 */ 39 @property (nonatomic, strong) SVUser *user; 40 /** 41 * 轉發的微博 42 */ 43 @property (nonatomic, strong) SVStatus *retweeted_status; 44 @end 1 #import <Foundation/Foundation.h> 2 3 @interface SVUser : NSObject 4 /** 5 * 用戶的ID 6 */ 7 @property (nonatomic, copy) NSString *idstr; 8 /** 9 * 用戶的昵稱 10 */ 11 @property (nonatomic, copy) NSString *name; 12 /** 13 * 用戶的頭像 14 */ 15 @property (nonatomic, copy) NSString *profile_image_url; 16 17 /** 18 * 會員等級 19 */ 20 @property (nonatomic, assign) int mbrank; 21 /** 22 * 會員類型 23 */ 24 @property (nonatomic, assign) int mbtype; 25 @end?
4.典型的cell高度不確定時,分析微博的結構,而微博的結構根據內容會有所不同,基本為三種:只有文字、文字和配圖、帶有轉發的微博。不管哪種,按照從上到下的順序,就可以將其分為三個部分:頭像昵稱、主要內容和轉評贊條。為了確定位置,會再添加一個計算frame的模型SVStatusFrame,用來計算每一個控件在整個cell的位置。在SVStatusFrame中只需要重寫status的setter方法,在給status設置值得時候,根據不同的值進行計算控件的frame,最后得出整個cell的高度。目前在SVStatusFrame的.h文件中喲很多關于字體、顏色的宏定義,還有目前對于頭文件的引用也不是很規范,在基本效果完成之后會將他們進行抽取。
1 @interface SVStatusFrame : NSObject 2 @property (nonatomic, strong) SVStatus *status; 3 4 /** 頂部的view */ 5 @property (nonatomic, assign, readonly) CGRect topViewF; 6 /** 頭像 */ 7 @property (nonatomic, assign, readonly) CGRect iconViewF; 8 /** 會員圖標 */ 9 @property (nonatomic, assign, readonly) CGRect vipViewF; 10 /** 配圖 */ 11 @property (nonatomic, assign, readonly) CGRect photoViewF; 12 /** 昵稱 */ 13 @property (nonatomic, assign, readonly) CGRect nameLabelF; 14 /** 時間 */ 15 @property (nonatomic, assign, readonly) CGRect timeLabelF; 16 /** 來源 */ 17 @property (nonatomic, assign, readonly) CGRect sourceLabelF; 18 /** 正文\內容 */ 19 @property (nonatomic, assign, readonly) CGRect contentLabelF; 20 21 /** 被轉發微博的view(父控件) */ 22 @property (nonatomic, assign, readonly) CGRect retweetViewF; 23 /** 被轉發微博作者的昵稱 */ 24 @property (nonatomic, assign, readonly) CGRect retweetNameLabelF; 25 /** 被轉發微博的正文\內容 */ 26 @property (nonatomic, assign, readonly) CGRect retweetContentLabelF; 27 /** 被轉發微博的配圖 */ 28 @property (nonatomic, assign, readonly) CGRect retweetPhotoViewF; 29 30 /** 微博的工具條 */ 31 @property (nonatomic, assign, readonly) CGRect statusToolbarF; 32 33 /** cell的高度 */ 34 @property (nonatomic, assign, readonly) CGFloat cellHeight;?
5.為了使cell不過于依賴控制器而存在,為其提供一個類方法來創建件cell,并在cell內部實現重用。在該方法中添加控件,添加控件分成了3個部分,其中的代碼很簡單的就是創建控件、設置只要不需改變的值、添加、保存起來。
1 // 全部的控件 2 @interface SVStatusCell() 3 /** 頂部的view */ 4 @property (nonatomic, weak) UIImageView *topView; 5 /** 頭像 */ 6 @property (nonatomic, weak) UIImageView *iconView; 7 /** 會員圖標 */ 8 @property (nonatomic, weak) UIImageView *vipView; 9 /** 配圖 */ 10 @property (nonatomic, weak) UIImageView *photoView; 11 /** 昵稱 */ 12 @property (nonatomic, weak) UILabel *nameLabel; 13 /** 時間 */ 14 @property (nonatomic, weak) UILabel *timeLabel; 15 /** 來源 */ 16 @property (nonatomic, weak) UILabel *sourceLabel; 17 /** 正文\內容 */ 18 @property (nonatomic, weak) UILabel *contentLabel; 19 20 /** 被轉發微博的view(父控件) */ 21 @property (nonatomic, weak) UIImageView *retweetView; 22 /** 被轉發微博作者的昵稱 */ 23 @property (nonatomic, weak) UILabel *retweetNameLabel; 24 /** 被轉發微博的正文\內容 */ 25 @property (nonatomic, weak) UILabel *retweetContentLabel; 26 /** 被轉發微博的配圖 */ 27 @property (nonatomic, weak) UIImageView *retweetPhotoView; 28 29 /** 微博的工具條 */ 30 @property (nonatomic, weak) UIImageView *statusToolbar; 31 @end?
1 + (instancetype)cellWithtableView:(UITableView *)tableView 2 { 3 static NSString *ID = @"status"; 4 SVStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 5 if (cell == nil) { 6 cell = [[SVStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 7 } 8 return cell; 9 } 10 11 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 12 { 13 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 14 if (self) { 15 // 1.添加原創微博內部的子控件 16 [self setupOriginalSubviews]; 17 18 // 2.添加被轉發微博內部的子控件 19 [self setupRetweetSubviews]; 20 21 // 3.添加微博的工具條 22 [self setupStatusToolBar]; 23 } 24 return self; 25 }6.對于給cell上的控件賦值數據同樣很簡單,就是重寫statusFrame的setter方法,再給他賦值的同時,將status數據傳遞過來。
1 /** 2 * 傳遞模型數據 3 */ 4 - (void)setStatusFrame:(SVStatusFrame *)statusFrame 5 { 6 _statusFrame = statusFrame; 7 8 // 1.原創微博 9 [self setupOriginalData]; 10 11 // 2.被轉發微博 12 [self setupRetweetData]; 13 14 // 3.微博工具條 15 [self setupStatusToolbar]; 16 }
轉載于:https://www.cnblogs.com/sleen/p/5236568.html
總結
- 上一篇: C++ multimap 的插入,遍历,
- 下一篇: 怎样使用Secure CRT查看vcen