多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 貓貓學IOS(十七)UI之純代碼自定義Cell實現新浪微博UI

貓貓學IOS(十七)UI之純代碼自定義Cell實現新浪微博UI

來源:程序員人生   發布時間:2015-04-22 08:36:00 閱讀次數:3666次

貓貓分享,必須精品

素材代碼地址:http://blog.csdn.net/u013357243/article/details/44976175
原文地址:http://blog.csdn.net/u013357243?viewmode=contents

先看效果圖

這里寫圖片描述

這里寫圖片描述

這里寫圖片描述


編程思路

代碼創建Cell的步驟

1> 創建自定義Cell,繼承自UITableViewCell
2> 根據需求,肯定控件,并定義屬性
3> 用getter方法完成控件的實例化,只創建并添加到contentView,不處理位置
4> 定義1個模型屬性,通過setter方法,設置cell的顯示

昵稱正文字符串的位置算法

設置大小由文字的長度決定 用字符串方法:[@"" boundingRectWithSize:(CGSize) options:(NSStringDrawingOptions) attributes:(NSDictionary *) context:(NSStringDrawingContext *)] //boundingRectWithSize計算給定文字字符串所占的區域,返回是1個x,y為0的CGRect // 如果要計算多行的準確高度需要傳入 // options:NSStringDrawingUsesLineFragmentOrigin //attribbutes:dict 用于指定字體的相干屬性的字典。UIKit框架的第1個頭文件ps 這個頭文件不記很難找 // context :nil #define kNameFont [UIFont systemFontOfSize:14] NSDictionary *nameDict = @{NSFontAttributeName:kNameFont}; CGRect nameFrame = [self.status.name boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil];

計算行高的方法

要用到代理方法的:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

計算行高的方法,會在加載表格數據時,有多少行計算多少次 contentSize

 問題:此方法履行的時候,cell還沒有被實例化!
 但是:行高計算是在實例化cell時,通過設置status屬性,計算=》有了status模型,就能夠知道行高  !

 問題:如何在cell實例化之前,取得行高?
 解決方法:通過status可以計算得到行高! = 》再建立1個模型,專門計算所有控件的位置

正告:原形單元格必須又1個可重用標示符的解決

正告:file:///Users/apple/Desktop/%E5%AD%A6%E4%B9%A0/%E4%BA%8C%E6%9C%9F%E5%AD%A6%E4%B9%A0/Day07/%E6%96%B0%E6%B5%AA%E5%BE%AE%E5%8D%9AUI/%E6%96%B0%E6%B5%AA%E5%BE%AE%E5%8D%9AUI/Base.lproj/Main.storyboard: warning: Unsupported Configuration: Prototype table cells must have reuse identifiers
正告:原形單元格必須又1個可重用標示符
解決辦法是在Cell中的Identfier加入可重用標示符
圖

然后1定要關聯cell的class
圖

――這兩部可以用1行代碼來代替

//為tableView注冊可重用單元格 [self.tableView registerClass:[NYStatusCell class] forCellReuseIdentifier:ID];

這時候候我們注釋

// if (cell == nil) { // cell = [[NYStatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; // }

也能夠運行了

    在Storyboard中指定了可重用標示符,同時指定了Cell的類是NYStatusCell,系統會為tableView注冊1個原形cell,專門用來做可重用單元格,1旦緩沖區不存在可重用單元格,系統會使用原形Cell新實例化1個Cell供程序使用!
因此如果在Storyb中,注冊了原形Cell,就不需要做 cell == nil 的判斷了

注意:這些在iOS6以后才有的。

代碼學習

類結構

這個小項目主要由這些類組成
這里寫圖片描述

MVC各自負責各自的東西
Model有兩個模型,1個是Status主要負責所有數據
Status中有

@property (nonatomic, copy)NSString *text; @property (nonatomic, copy)NSString *icon; @property (nonatomic, copy)NSString *name; @property (nonatomic, assign)BOOL vip; @property (nonatomic, copy) NSString *picture;

這些屬性主要包括頭像,昵稱,vip圖標,正文,圖片
StatusFrame模型主要負責寄存每個組件在cell所要寄存的位置

//提高安全性能:+readonly @property (nonatomic, assign, readonly)CGRect textF; @property (nonatomic, assign, readonly)CGRect iconF; @property (nonatomic, assign, readonly)CGRect nameF; @property (nonatomic, assign, readonly)CGRect vipF; @property (nonatomic, assign, readonly)CGRect pictureF; /**行高*/ @property (nonatomic, assign)CGFloat cellHeight; /**所有控件的尺寸都可以通過Status來計算得出*/ @property (nonatomic, strong)NYStatus *status;

NYViewController中的代碼

//ps:新建iOS交換學習群:304570962 可以加貓貓QQ:1764541256 或則微信znycat 讓我們1起努力學習吧。 原文:http://blog.csdn.net/u013357243?viewmode=contents // NYViewController.m // 新浪微博UI // // Created by apple on 15⑷⑻. // Copyright (c) 2015年 znycat. All rights reserved. // #import "NYViewController.h" #import "NYStatus.h" #import "NYStatusCell.h" #import "NYStatusFrame.h" @interface NYViewController () @property (nonatomic, strong) NSArray *statusFrames; @end @implementation NYViewController static NSString *ID = @"Cell"; /**懶加載數據*/ -(NSArray *)statusFrames { if (_statusFrames == nil) { _statusFrames = [NYStatusFrame statusFrames]; } return _statusFrames; } -(void)viewDidLoad { [super viewDidLoad]; self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0); //為tableView注冊可重用單元格 [self.tableView registerClass:[NYStatusCell class] forCellReuseIdentifier:ID]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - 數據源方法 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.statusFrames.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /** 在Storyboard中指定了可重用標示符,同時指定了Cell的類是HMStatusCell 系統會為tableView注冊1個原形Cell,專門用來做可重用單元格的,1旦緩沖區中不存在 可重用單元格,系統會使用原形Cell新實例化1個Cell用程序使用! 因此如果在,Storyboard中,注冊了原形Cell,就不再需要 cell == nil的判斷了 */ // NYStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard // 使用這個方法,要求1定注冊可重用單元格,否則就會崩潰! // 官方建議使用以下方法,利用程序的崩潰,及時發現問題 NYStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath]; // if (cell == nil) { // cell = [[NYStatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; // } NYStatusFrame *statusFrame = self.statusFrames[indexPath.row]; cell.statusFrame = statusFrame; return cell; } #pragma mark - 代理方法 /**計算單元格行高*/ -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { /** 計算行高的方法,會在加載表格數據時,有多少行計算多少次 contentSize 問題:此方法履行的時候,cell還沒有被實例化! 但是:行高計算是在實例化cell時,通過設置status屬性,計算=》有了status模型,就能夠知道行高 ! 問題:如何在cell實例化之前,取得行高? 解決方法:通過status可以計算得到行高! = 》再建立1個模型,專門計算所有控件的位置 */ NYStatusFrame *statusFrame = self.statusFrames[indexPath.row]; return statusFrame.cellHeight; } @end

自定義cell純代碼寫NYStatusCell

// // NYStatusCell.m // 新浪微博UI // // Created by apple on 15⑷⑼. // Copyright (c) 2015年 znycat. All rights reserved. // #import "NYStatusCell.h" #import "NYStatus.h" #import "NYStatusFrame.h" /**姓名字體*/ #define kNameFont [UIFont systemFontOfSize:14] /**正文字體*/ #define kTextFont [UIFont systemFontOfSize:16] @interface NYStatusCell() //1>創建自定iyiCell,繼承自UITableViewCell //2>根據需求,肯定控件,并定義屬性。 @property (nonatomic, strong) UIImageView *iconView; @property (nonatomic, strong) UILabel *nameView; @property (nonatomic, strong) UIImageView *vipView; @property (nonatomic, strong) UILabel *textView; @property (nonatomic, strong) UIImageView *pictureView; @end @implementation NYStatusCell //3>用get方法完成控件的實例化,只創建并添加到contentView,不處理位置。 -(UIImageView *)iconView { if (_iconView == nil) { _iconView = [[UIImageView alloc] init]; [self.contentView addSubview:_iconView]; } return _iconView; } -(UILabel *)nameView { if (_nameView == nil) { _nameView = [[UILabel alloc] init]; //默許字體是17號,改成kNameFont _nameView.font = kNameFont; [self.contentView addSubview:_nameView]; } return _nameView; } -(UIImageView *)vipView { if (_vipView == nil) { _vipView = [[UIImageView alloc] init]; [self.contentView addSubview:_vipView]; } return _vipView; } -(UILabel *)textView { if (_textView == nil) { _textView = [[UILabel alloc] init]; _textView.font = kTextFont; _textView.numberOfLines = 0;//讓他可以換行 [self.contentView addSubview:_textView]; } return _textView; } -(UIImageView *)pictureView { if (_pictureView == nil) { _pictureView = [[UIImageView alloc] init]; [self.contentView addSubview:_pictureView]; } return _pictureView; } -(void)setStatusFrame:(NYStatusFrame *)statusFrame { _statusFrame = statusFrame; //1>設置數據 [self settingData]; //2>設置位置 [self settingFrame]; } /**設置數據*/ -(void)settingData { NYStatus *status = self.statusFrame.status; //頭像 self.iconView.image = [UIImage imageNamed:status.icon]; //姓名 self.nameView.text = status.name; //vip if (status.vip) { self.vipView.image = [UIImage imageNamed:@"vip"]; } //內容正文 self.textView.text = status.text; //圖片可選參數: if (status.picture.length > 0) { self.pictureView.hidden = YES; self.pictureView.image = [UIImage imageNamed:status.picture]; } self.pictureView.hidden = NO; } /**設置位置*/ -(void)settingFrame { //1.頭像 self.iconView.frame = self.statusFrame.iconF; //2,姓名大小由文字的長度決定 //boundingRectWithSize計算給定文字字符串所占的區域,返回是1個x,y為0的CGRect;w,h是計算好的寬高 // 如果要計算多行的準確高度需要傳入 // options:NSStringDrawingUsesLineFragmentOrigin //attribbutes:dict 用于指定字體的相干屬性的字典。UIKit框架的第1個頭文件ps 這個頭文件不記很難找 // context :nil self.nameView.frame = self.statusFrame.nameF; //3,vip圖片 self.vipView.frame = self.statusFrame.vipF; //4,正文 self.textView.frame = self.statusFrame.textF; //5,圖片 self.pictureView.frame = self.statusFrame.pictureF; } @end

模型實現方法

NYStatus

// // NYStatus.m // 新浪微博UI // // Created by apple on 15⑷⑻. // Copyright (c) 2015年 znycat. All rights reserved. // #import "NYStatus.h" @implementation NYStatus -(instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dict]; } return self; } +(instancetype)statusWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } +(NSArray *)statuses { NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { [arrayM addObject:[self statusWithDict:dict]]; } return arrayM; } @end

NYStatusFrame

// // NYStatusFrame.m // 新浪微博UI // // Created by apple on 15⑷⑼. // Copyright (c) 2015年 znycat. All rights reserved. // #import "NYStatusFrame.h" #import "NYStatus.h" #import "NSString+Tools.h" #define kNameFont [UIFont systemFontOfSize:14] /**正文字體*/ #define kTextFont [UIFont systemFontOfSize:16] @implementation NYStatusFrame /** 為了程序的安全(面向對象的思想,你給我你就相信我,讓我來改,他人別隨意動) 為了讓程序更安全,我們將類NYStatusFrame的有關位置的:(例如iconF)設置成readonly只讀屬性,這時候候我們用self.iconF(_iconF的set方法)的時候就不能用了。 此時要注意: 1旦重寫了readonly屬性的getter方法, -(CGRect)iconF { } 帶_的成員變量就不存在了 這時候候如果還需要使用_成員變量,則必須用合成指令@synthesize @synthesize iconF = _iconF; */ -(void)setStatus:(NYStatus *)status { _status = status; //0.定義間距 CGFloat padding = 10; //1.頭像 CGFloat iconX = padding; CGFloat iconY = padding; CGFloat iconW = 30; CGFloat iconH = 30; _iconF = CGRectMake(iconX, iconY, iconW, iconH); //2,姓名大小由文字的長度決定 //boundingRectWithSize計算給定文字字符串所占的區域,返回是1個x,y為0的CGRect;w,h是計算好的寬高 // 如果要計算多行的準確高度需要傳入 // options:NSStringDrawingUsesLineFragmentOrigin //attribbutes:dict 用于指定字體的相干屬性的字典。UIKit框架的第1個頭文件ps 這個頭文件不記很難找 // context :nil NSDictionary *nameDict = @{NSFontAttributeName:kNameFont}; // CGRect nameFrame = [self.status.name boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil]; CGRect nameFrame = [self.status.name textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:nameDict]; nameFrame.origin.x = CGRectGetMaxX(self.iconF) +padding; nameFrame.origin.y = padding + (self.iconF.size.height - nameFrame.size.height)*0.5; _nameF = nameFrame; //3,vip圖片 CGFloat vipX = CGRectGetMaxX(self.nameF) + padding; CGFloat vipY = self.nameF.origin.y; CGFloat vipW = 14; CGFloat vipH = 14; _vipF = CGRectMake(vipX, vipY, vipW, vipH); //4,正文 NSDictionary *textDict = @{NSFontAttributeName:kTextFont}; // CGRect textFrame = [self.status.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil]; CGRect textFrame = [self.status.text textRectWithSize:CGSizeMake(300, MAXFLOAT) attributes:textDict]; textFrame.origin.x = padding; textFrame.origin.y = CGRectGetMaxY(self.iconF) + padding; _textF = textFrame; //5,配圖 if (self.status.picture.length>0) { CGFloat pictureX = padding; CGFloat pictureY = CGRectGetMaxY(self.iconF) + padding +self.textF.size.height +padding; CGFloat pictureW = 100; CGFloat pictureH = 100; _pictureF= CGRectMake(pictureX, pictureY, pictureW, pictureH); self.cellHeight = CGRectGetMaxY(self.pictureF) + padding; }else{ self.cellHeight = CGRectGetMaxY(self.textF) + padding; } } +(NSArray *)statusFrames { NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { //要添加statusesFrame對象 NYStatusFrame *statusFrame = [[NYStatusFrame alloc]init]; //調用statusFrame的setter方法,保存status模型,同時計算出控件的位置(setter方法中進行的) statusFrame.status = [NYStatus statusWithDict:dict]; //將statusFrame添加到數組 [arrayM addObject:statusFrame]; } return arrayM; } @end

ps:新建iOS交換學習群:304570962
可以加貓貓QQ:1764541256 或則微信znycat
讓我們1起努力學習吧。
原文:http://blog.csdn.net/u013357243?viewmode=contents

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 中文字幕第一 | 国产亚洲欧美另类一区二区三区 | 日本欧美一区二区三区 | 亚洲成人99 | 另类一区二区三区 | 欧美一级aa免费毛片 | α毛片 | 亚洲成a人片在线v观看 | 国产一级精品绿帽视频 | 美女啪啪国产 | 99久久免费国内精品 | 亚洲精品色一区二区三区 | 中文字幕在线免费观看 | 欧美性xxxx极品高清 | 黑人一区二区三区中文字幕 | 在线午夜视频 | 国产精品欧美日韩 | 操你网站 | 在线看黄色网址 | 久久久久久久国产精品视频 | 日本一级级特黄特色大片 | 欧美日本一级在线播放 | 欧美性活生活视频 | 亚洲欧美专区 | 女女女女女女bbbbbb毛片 | 中文字幕乱码中文乱码51精品 | 欧美极品xxxxⅹ另类 | japanesehd国产在线无毒不卡 | 一级白嫩美女毛片免费 | 欧美日本在线观看 | 好大好湿好硬顶到了好爽在 | jizz性欧美2| 禁视频网站在线观看漫画 | 日本香蕉一区二区三区 | 性freemovies中国群众 | 国产精品久久久久久久毛片 | 欧美一级www | 中文一区二区 | 国产日韩精品欧美一区喷 | 日本成人不卡 | 日本中文字幕永久在线 |