UITableViewCell控件空間構(gòu)造
- cell的子控件是contentView笑旺,contentView的子控件是imageView胰柑、textLabel、detailTextLabel
- 可以通過(guò)移動(dòng)contentView污筷,顯示刪除按鈕证薇,點(diǎn)擊刪除按鈕便可實(shí)現(xiàn)刪除對(duì)應(yīng)的cell
- 在代碼方式制作等高cell中,cell可以通過(guò)AutoLayOut(約束)在layoutSubView或initWithStyle方法中設(shè)置子控件的位置
-
靜態(tài)cell的數(shù)據(jù)是死的且可以自定義效拭,動(dòng)態(tài)cell數(shù)據(jù)顯示源自模型數(shù)據(jù)庫(kù)
Snip20170205_143.png -
設(shè)置tableView每 cell的度,默認(rèn)是44
Snip20170205_136.png
Snip20170205_137.png
UITableView
-
表頂部圖片顯示:將圖片控件添加到表頭視圖上
Snip20170205_144.png
self.tableView.tableHeaderView
- 凡是遵守UITableViewDataSource協(xié)議的OC對(duì)象暂吉,都可以是UITableView的數(shù)據(jù)源
-
Control負(fù)責(zé)初始化Model胖秒,并將Model傳遞給View去解析展示
Snip20170205_140.png
Cell的重用原理
- 重用原理:
- 當(dāng)滾動(dòng)列表時(shí)慕的,部分UITableViewCell會(huì)移出窗口阎肝,UITableView會(huì)將窗口外的UITableViewCell放入一個(gè)對(duì)象池中,等待重用肮街。當(dāng)UITableView要求dataSource返回UITableViewCell時(shí)风题,dataSource會(huì)先查看這個(gè)對(duì)象池,如果池中有未使用的UITableViewCell嫉父,dataSource會(huì)用新的數(shù)據(jù)配置這個(gè)UITableViewCell沛硅,然后返回給UITableView,重新顯示到窗口中绕辖,從而避免創(chuàng)建新對(duì)象
- 解決方案:
- UITableViewCell有個(gè)NSString *reuseIdentifier屬性摇肌,可以在初始化UITableViewCell的時(shí)候傳入一個(gè)特定的字符串標(biāo)識(shí)來(lái)設(shè)置reuseIdentifier(一般用UITableViewCell的類(lèi)名)。當(dāng)UITableView要求dataSource返回UITableViewCell時(shí)仪际,先通過(guò)一個(gè)字符串標(biāo)識(shí)到對(duì)象池中查找對(duì)應(yīng)類(lèi)型的UITableViewCell對(duì)象围小,如果有,就重用树碱,如果沒(méi)有肯适,就傳入這個(gè)字符串標(biāo)識(shí)來(lái)初始化一個(gè)UITableViewCell對(duì)象
Cell的重用代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.定義一個(gè)cell的標(biāo)識(shí)
static NSString *ID = @”cell";
// 2.從緩存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果緩存池中沒(méi)有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
通過(guò)代碼自定義cell(cell的高度不一致)
- 新建一個(gè)繼承自UITableViewCell的類(lèi)
- 重寫(xiě)initWithStyle:reuseIdentifier:方法
- 添加所有需要顯示的子控件(不需要設(shè)置子控件的數(shù)據(jù)和frame, 子控件要添加到contentView中)
- 進(jìn)行子控件一次性的屬性設(shè)置(有些屬性只需要設(shè)置一次, 比如字體固定的圖片)
- 提供2個(gè)模型
- 數(shù)據(jù)模型: 存放文字?jǐn)?shù)據(jù)\圖片數(shù)據(jù)
- frame模型: 存放數(shù)據(jù)模型\所有子控件的frame\cell的高度
- cell擁有一個(gè)frame模型(不要直接擁有數(shù)據(jù)模型)
- 重寫(xiě)frame模型屬性的setter方法: 在這個(gè)方法中設(shè)置子控件的顯示數(shù)據(jù)和frame
- frame模型數(shù)據(jù)的初始化已經(jīng)采取懶加載的方式(每一個(gè)cell對(duì)應(yīng)的frame模型數(shù)據(jù)只加載一次)
UITextField
- 通過(guò)UITextField的代理方法能夠監(jiān)聽(tīng)鍵盤(pán)最右下角按鈕的點(diǎn)擊
- 成為UITextField的代理
self.textField.delegate = self;
- 遵守UITextFieldDelegate協(xié)議,實(shí)現(xiàn)代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- 在UITextField左邊放一個(gè)view
self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 0)];
self.textField.leftViewMode = UITextFieldViewModeAlways;
索引條
- 設(shè)置索引條上 字顏
self.tableView.sectionIndexColor = [UIColor redColor];
- 設(shè)置索引條的背景顏
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
- 返回每 組的索引標(biāo)題(數(shù)組中都是NSString對(duì)象)
- (NSArray<NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView {
}
Frame自定義不等高的cell
- 給模型增加frame數(shù)據(jù)
- 所有子控件的frame
- cell的高度
@interface XMGStatus : NSObject
/** 頭像的frame */
@property (nonatomic, assign) CGRect iconFrame;
// .....
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
- 重寫(xiě)模型cellH屬性的get方法
- (CGFloat)cellHeight{
if (_cellHeight == 0) {
// ... 計(jì)算所有子控件的frame、cell的高度
}
return _cellHeight;
}
- 在控制器中
- 實(shí)現(xiàn)一個(gè)返回cell高度的代理方法
- 在這個(gè)方法中返回indexPath位置對(duì)應(yīng)cell的高度
/**
返回每一行cell的具體高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
XMGStatus *status = self.statuses[indexPath.row];
return status.cellH;
}
- 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"status";
// 訪問(wèn)緩存池
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.status = self.statuses[indexPath.row];
return cell;
}
- 新建一個(gè)繼承自UITableViewCell的子類(lèi)赴恨,比如XMGStatusCell
@interface XMGStatusCell : UITableViewCell
@end
- 在XMGStatusCell.m文件中
- 重寫(xiě)-initWithStyle:reuseIdentifier:方法
- 在這個(gè)方法中添加所有可能顯示的子控件
- 給子控件做一些初始化設(shè)置(設(shè)置字體疹娶、文字顏色等)
/**
* 在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}
- 在XMGStatusCell.h文件中提供一個(gè)模型屬性,比如XMGStatus模型
@class XMGStatus;
@interface XMGStatusCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) XMGStatus *status;
@end
- 在XMGStatusCell.m文件中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setStatus:(XMGStatus *)status{
_status = status;
// .......
}
- 重寫(xiě)-layoutSubviews方法
- 一定要調(diào)用[super layoutSubviews]
- 在這個(gè)方法中設(shè)置所有子控件的frame
/**
* 在這個(gè)方法中設(shè)置所有子控件的frame
*/
- (void)layoutSubviews{
[super layoutSubviews];
// ......
}
AutoLayOut自定義等高的cell
- 新建一個(gè)繼承自UITableViewCell的子類(lèi)伦连,比如XMGTgCell
@interface XMGTgCell : UITableViewCell
@end
- 在XMGTgCell.m文件中
- 重寫(xiě)-initWithStyle:reuseIdentifier:方法
- 在這個(gè)方法中添加所有的子控件
- 給子控件做一些初始化設(shè)置(設(shè)置字體雨饺、文字顏色等)
- 添加子控件的完整約束
/**
* 在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}
- 在XMGTgCell.h文件中提供一個(gè)模型屬性,比如XMGTg模型
@class XMGTg;
@interface XMGTgCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) XMGTg *tg;
@end
- 在XMGTgCell.m中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(XMGTg *)tg{
_tg = tg;
// .......
}
- 在控制器中
- 注冊(cè)cell的類(lèi)型
[self.tableView registerClass:[XMGTgCell class] forCellReuseIdentifier:ID];
- 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 訪問(wèn)緩存池
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.tg = self.tgs[indexPath.row];
return cell;
}
字典轉(zhuǎn)模型第三方框架
- Mantle
- 所有模型都必須繼承自MTModel
- JSONModel
- 所有模型都必須繼承自JSONModel
- MJExtension
- 不需要強(qiáng)制繼承任何其他類(lèi)
Frame(微博)自定義等高的cell
- 新建一個(gè)繼承自UITableViewCell的子類(lèi)惑淳,比如XMGTgCell
@interface XMGTgCell : UITableViewCell
@end
- 在XMGTgCell.m文件中
- 重寫(xiě)-initWithStyle:reuseIdentifier:方法
- 在這個(gè)方法中添加所有的子控件
- 給子控件做一些初始化設(shè)置(設(shè)置字體额港、文字顏色等)
/**
* 在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}
- 重寫(xiě)-layoutSubviews方法
- 一定要調(diào)用[super layoutSubviews]
- 在這個(gè)方法中計(jì)算和設(shè)置所有子控件的frame
/**
* 在這個(gè)方法中計(jì)算所有子控件的frame
*/
- (void)layoutSubviews{
[super layoutSubviews];
// ......
}
- 在XMGTgCell.h文件中提供一個(gè)模型屬性,比如XMGTg模型
@class XMGTg;
@interface XMGTgCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) XMGTg *tg;
@end
- 在XMGTgCell.m中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(XMGTg *)tg{
_tg = tg;
// .......
}
- 在控制器中
- 注冊(cè)cell的類(lèi)型
[self.tableView registerClass:[XMGTgCell class] forCellReuseIdentifier:ID];
- 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 訪問(wèn)緩存池
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.tg = self.tgs[indexPath.row];
return cell;
}
StoryBoard(微博)自定義等高的cell
- 新建一個(gè)繼承自
UITableViewCell
的子類(lèi)歧焦,比如XMGTgCell
@interface XMGTgCell : UITableViewCell
@end
- 在storyboard文件中移斩,找到UITableView里面的cell(動(dòng)態(tài)cell)
- 修改cell的class為XMGTgCell
- 綁定循環(huán)利用標(biāo)識(shí)
- 添加子控件,設(shè)置子控件約束
- 將子控件連線到類(lèi)擴(kuò)展中
@interface XMGTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
- 在XMGTgCell.h文件中提供一個(gè)模型屬性绢馍,比如XMGTg模型
@class XMGTg;
@interface XMGTgCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) XMGTg *tg;
@end
- 在XMGTgCell.m中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(XMGTg *)tg{
_tg = tg;
// .......
}
- 在控制器中
- 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"tg";
// 訪問(wèn)緩存池
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.tg = self.tgs[indexPath.row];
return cell;
}
xib自定義等高cell
- 新建一個(gè)繼承自
UITableViewCell
的子類(lèi)向瓷,比如XMGTgCell
@interface XMGTgCell : UITableViewCell
@end
- 新建一個(gè)xib文件(文件名最好跟類(lèi)名一致,比如XMGTgCell.xib)
- 修改cell的class為XMGTgCell
- 綁定循環(huán)利用標(biāo)識(shí)
- 添加子控件舰涌,設(shè)置子控件約束
- 將子控件連線到類(lèi)擴(kuò)展中
@interface XMGTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
- 在XMGTgCell.h文件中提供一個(gè)模型屬性猖任,比如XMGTg模型
@class XMGTg;
@interface XMGTgCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) XMGTg *tg;
@end
- 在XMGTgCell.m中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(XMGTg *)tg{
_tg = tg;
// .......
}
- 在控制器中
- 注冊(cè)xib文件
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
- 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 訪問(wèn)緩存池
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.tg = self.tgs[indexPath.row];
return cell;
}
StoryBoard(微博)-自定義不等的cell
- 對(duì)比自定義等高cell,需要幾個(gè)額外的步驟(iOS8開(kāi)始才支持)
- 添加子控件和contentView之間的間距約束
- 設(shè)置tableViewCell的真實(shí)行高和估算行高
//告訴tableView所有cell的真實(shí)高度是自動(dòng)計(jì)算(根據(jù)設(shè)置的約束來(lái)計(jì)算)
self.tableView.rowHeight = UITableViewAutomaticDimension;
//告訴tableView所有cell的估算高度
self.tableView.estimatedRowHeight = 44;
- 如果要支持iOS8之前
- 如果cell內(nèi)部有自動(dòng)換行的label瓷耙,需要設(shè)置preferredMaxLayoutWidth屬性
-(void)awakeFromNib
{
//手動(dòng)設(shè)置文字的最大寬度(目的是:讓label知道自己文字的最大寬度朱躺,進(jìn)而能夠計(jì)算出自己的frame)
self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
- 設(shè)置tableView的cell估算高度
//告訴tableView所有cell的估算高度(設(shè)置了估算高度刁赖,就可以減少tableView:heightForRowAtIndexPath:方法的調(diào)用次數(shù))
self.tableView.estimatedRowHeight = 200;
- 在代理方法中計(jì)算cell的高度
XMGStatusCell *cell;
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//創(chuàng)建一個(gè)臨時(shí)的cell(cell的作用:根據(jù)模型數(shù)據(jù)布局所有的子控件,進(jìn)而計(jì)算出cell的高度)
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
}
//設(shè)置模型數(shù)據(jù)
cell.status = self.statuses[indexPath.row];
return cell.height;
}
-(CGFloat)height{
//強(qiáng)制布局cell內(nèi)部的所有子控件(label根據(jù)文字多少計(jì)算出自己最真實(shí)的尺寸)
[self layoutIfNeeded];
//計(jì)算cell的高度
if (self.status.picture) {
return CGRectGetMaxY(self.pictureImageView.frame) + 10;
} else {
return CGRectGetMaxY(self.text_label.frame) + 10;
}
}
數(shù)據(jù)刷新
添加數(shù)據(jù)
刪除數(shù)據(jù)
更改數(shù)據(jù)
全局刷新方法(最常用)
[self.tableView reloadData];
//屏幕上的所有可視的cell都會(huì)刷新一遍
-
局部刷新方法
- 添加數(shù)據(jù)
NSArray *indexPaths = @[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
- 刪除數(shù)據(jù)
NSArray *indexPaths = @[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
- 更新數(shù)據(jù)(沒(méi)有添加和刪除數(shù)據(jù)长搀,僅僅是修改已經(jīng)存在的數(shù)據(jù))
NSArray *indexPaths = @[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
-
左滑出現(xiàn)刪除按鈕
- 需要實(shí)現(xiàn)tableView的代理方法
/**
*只要實(shí)現(xiàn)了這個(gè)方法宇弛,左滑出現(xiàn)Delete按鈕的功能就有了
*點(diǎn)擊了“左滑出現(xiàn)的Delete按鈕”會(huì)調(diào)用這個(gè)方法
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//刪除模型
[self.wineArray removeObjectAtIndex:indexPath.row];
//刷新
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
/**
*修改Delete按鈕文字為“刪除”
*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
左滑出現(xiàn)N個(gè)按鈕
需要實(shí)現(xiàn)tableView的代理方法
/**
*只要實(shí)現(xiàn)了這個(gè)方法,左滑出現(xiàn)按鈕的功能就有了
(一旦左滑出現(xiàn)了N個(gè)按鈕源请,tableView就進(jìn)入了編輯模式, tableView.editing = YES)
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
/**
*左滑cell時(shí)出現(xiàn)什么按鈕
*/
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關(guān)注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點(diǎn)擊了關(guān)注");
//收回左滑出現(xiàn)的按鈕(退出編輯模式)
tableView.editing = NO;
}];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.wineArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return @[action1, action0];
}
- 進(jìn)入編輯模式
// self.tabelView.editing = YES;
[self.tableView setEditing:YES animated:YES];
//默認(rèn)情況下枪芒,進(jìn)入編輯模式時(shí),左邊會(huì)出現(xiàn)一排紅色的“減號(hào)”按鈕
- 在編輯模式中多選
//編輯模式的時(shí)候可以多選
self.tableView.allowsMultipleSelectionDuringEditing = YES;
//進(jìn)入編輯模式
[self.tableView setEditing:YES animated:YES];
//獲得選中的所有行
self.tableView.indexPathsForSelectedRows;
代理的使用步驟
- 定義一份代理協(xié)議
- 協(xié)議名字的格式一般是:類(lèi)名+ Delegate
- 比如UITableViewDelegate
- 設(shè)計(jì)代理的細(xì)節(jié)
- 一般都是@optional(讓代理可以有選擇性去實(shí)現(xiàn)一些代理方法)
- 方法名一般都以類(lèi)名開(kāi)頭
- 比如- (void)scrollViewDidScroll:
- 一般都需要將對(duì)象本身傳出去
- 比如tableView的代理方法都會(huì)把tableView本身傳出去
必須要遵守NSObject協(xié)議(基協(xié)議)
比如@protocol XMGWineCellDelegate <NSObject>
-
聲明一個(gè)代理屬性
- 代理的類(lèi)型格式:id<協(xié)議> delegate
@property (nonatomic, weak) id<XMGWineCellDelegate> delegate;
設(shè)置代理對(duì)象
代理對(duì)象遵守協(xié)議巢钓,實(shí)現(xiàn)協(xié)議里面相應(yīng)的方法
當(dāng)控件內(nèi)部發(fā)生了一些事情病苗,就可以調(diào)用代理的代理方法通知代理
- 如果代理方法是@optional,那么需要判斷方法是否有實(shí)現(xiàn),直接調(diào)用可能會(huì)報(bào)錯(cuò)
if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {
[self.delegate wineCellDidClickPlusButton:self];
}
iOS監(jiān)聽(tīng)某些事件的方法
- 通知(NSNotificationCenter\NSNotification)
- 任何對(duì)象之間都可以傳遞消息
- 使用范圍
- 1個(gè)對(duì)象可以發(fā)通知給多個(gè)對(duì)象
- 1個(gè)對(duì)象可以接受多個(gè)對(duì)象發(fā)出的通知
- 要求:必須得保證通知的名字在發(fā)出和監(jiān)聽(tīng)時(shí)是一致的
- KVO
- 僅僅是能監(jiān)聽(tīng)對(duì)象屬性的改變(靈活度不如通知和代理)
- 代理
- 使用范圍
- 1個(gè)對(duì)象只能設(shè)置一個(gè)代理(假設(shè)這個(gè)對(duì)象只有1個(gè)代理屬性)
- 1個(gè)對(duì)象能成為多個(gè)對(duì)象的代理
- 如何選擇?
- 代理比通知規(guī)范
- 建議使用代理多于通知,能使用代理盡量使用代理
通知
-
基本概念
每一個(gè)應(yīng)用程序都有一個(gè)通知中心(NSNotificationCenter)實(shí)例症汹,專(zhuān)門(mén)負(fù)責(zé)協(xié)助不同對(duì)象之間的消息通信
任何一個(gè)對(duì)象都可以向通知中心發(fā)布通知(NSNotification)硫朦,描述自己在做什么。其他感興趣的對(duì)象(Observer)可以申請(qǐng)?jiān)谀硞€(gè)特定通知發(fā)布時(shí)(或在某個(gè)特定的對(duì)象發(fā)布通知時(shí))收到這個(gè)通知
-
一個(gè)完整的通知一般包含3個(gè)屬性:
-(NSString *)name; // 通知的名稱
-(id)object; // 通知發(fā)布者(是誰(shuí)要發(fā)布通知)
-(NSDictionary *)userInfo; // 一些額外的信息(通知發(fā)布者傳遞給通知接收者的信息內(nèi)容)
-
初始化一個(gè)通知(NSNotification)對(duì)象
+(instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
+(instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
-(instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;
-
發(fā)布通知
通知中心(NSNotificationCenter)提供了相應(yīng)的方法來(lái)幫助發(fā)布通知
-
-(void)postNotification:(NSNotification *)notification;
- 發(fā)布一個(gè)notification通知背镇,可在notification對(duì)象中設(shè)置通知的名稱咬展、通知發(fā)布者、額外信息等
-
-(void)postNotificationName:(NSString *)aName object:(id)anObject;
- 發(fā)布一個(gè)名稱為aName的通知瞒斩,anObject為這個(gè)通知的發(fā)布者
-
-(void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
- 發(fā)布一個(gè)名稱為aName的通知破婆,anObject為這個(gè)通知的發(fā)布者,aUserInfo為額外信息
-
注冊(cè)通知監(jiān)聽(tīng)器
- 通知中心(NSNotificationCenter)提供了方法來(lái)注冊(cè)一個(gè)監(jiān)聽(tīng)通知的監(jiān)聽(tīng)器(Observer)
- -(void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
- observer:監(jiān)聽(tīng)器胸囱,即誰(shuí)要接收這個(gè)通知
- aSelector:收到通知后祷舀,回調(diào)監(jiān)聽(tīng)器的這個(gè)方法,并且把通知對(duì)象當(dāng)做參數(shù)傳入
- aName:通知的名稱。如果為nil,那么無(wú)論通知的名稱是什么盏缤,監(jiān)聽(tīng)器都能收到這個(gè)通知
- anObject:通知發(fā)布者壳影。如果為anObject和aName都為nil瓦阐,監(jiān)聽(tīng)器都收到所有的通知
- -(id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
- name:通知的名稱
- obj:通知發(fā)布者
- block:收到對(duì)應(yīng)的通知時(shí),會(huì)回調(diào)這個(gè)block
- queue:決定了block在哪個(gè)操作隊(duì)列中執(zhí)行,如果傳nil,默認(rèn)在當(dāng)前操作隊(duì)列中同步執(zhí)行
- 通知中心(NSNotificationCenter)提供了方法來(lái)注冊(cè)一個(gè)監(jiān)聽(tīng)通知的監(jiān)聽(tīng)器(Observer)
-
取消注冊(cè)通知監(jiān)聽(tīng)器
通知中心不會(huì)保留(retain)監(jiān)聽(tīng)器對(duì)象冤吨,在通知中心注冊(cè)過(guò)的對(duì)象,必須在該對(duì)象釋放前取消注冊(cè)饶套。否則漩蟆,當(dāng)相應(yīng)的通知再次出現(xiàn)時(shí),通知中心仍然會(huì)向該監(jiān)聽(tīng)器發(fā)送消息妓蛮。因?yàn)橄鄳?yīng)的監(jiān)聽(tīng)器對(duì)象已經(jīng)被釋放了爆安,所以可能會(huì)導(dǎo)致應(yīng)用崩潰
-
通知中心提供了相應(yīng)的方法來(lái)取消注冊(cè)監(jiān)聽(tīng)器
- -(void)removeObserver:(id)observer;
- -(void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
-
一般在監(jiān)聽(tīng)器銷(xiāo)毀之前取消注冊(cè)(如在監(jiān)聽(tīng)器中加入下列代碼):
- -(void)dealloc {
//[super dealloc]; 非ARC中需要調(diào)用此句
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- -(void)dealloc {
-
UIDevice通知
- UIDevice類(lèi)提供了一個(gè)單粒對(duì)象,它代表著設(shè)備仔引,通過(guò)它可以獲得一些設(shè)備相關(guān)的信息扔仓,比如電池電量值(batteryLevel)、電池狀態(tài)(batteryState)咖耘、設(shè)備的類(lèi)型(model翘簇,比如iPod、iPhone等)儿倒、設(shè)備的系統(tǒng)(systemVersion)
通過(guò)[UIDevice currentDevice]可以獲取這個(gè)單粒對(duì)象
-
UIDevice對(duì)象會(huì)不間斷地發(fā)布一些通知版保,下列是UIDevice對(duì)象所發(fā)布通知的名稱常量:
- UIDeviceOrientationDidChangeNotification // 設(shè)備旋轉(zhuǎn)
- UIDeviceBatteryStateDidChangeNotification // 電池狀態(tài)改變
- UIDeviceBatteryLevelDidChangeNotification // 電池電量改變
- UIDeviceProximityStateDidChangeNotification // 近距離傳感器(比如設(shè)備貼近了使用者的臉部)
-
鍵盤(pán)通知
我們經(jīng)常需要在鍵盤(pán)彈出或者隱藏的時(shí)候做一些特定的操作,因此需要監(jiān)聽(tīng)鍵盤(pán)的狀態(tài)
-
鍵盤(pán)狀態(tài)改變的時(shí)候,系統(tǒng)會(huì)發(fā)出一些特定的通知
- UIKeyboardWillShowNotification // 鍵盤(pán)即將顯示
- UIKeyboardDidShowNotification // 鍵盤(pán)顯示完畢
- UIKeyboardWillHideNotification // 鍵盤(pán)即將隱藏
- UIKeyboardDidHideNotification // 鍵盤(pán)隱藏完畢
- UIKeyboardWillChangeFrameNotification // 鍵盤(pán)的位置尺寸即將發(fā)生改變
- UIKeyboardDidChangeFrameNotification // 鍵盤(pán)的位置尺寸改變完畢
-
系統(tǒng)發(fā)出鍵盤(pán)通知時(shí),會(huì)附帶一下跟鍵盤(pán)有關(guān)的額外信息(字典),字典常見(jiàn)的key如下:
- UIKeyboardFrameBeginUserInfoKey // 鍵盤(pán)剛開(kāi)始的frame
- UIKeyboardFrameEndUserInfoKey // 鍵盤(pán)最終的frame(動(dòng)畫(huà)執(zhí)行完畢后)
- UIKeyboardAnimationDurationUserInfoKey // 鍵盤(pán)動(dòng)畫(huà)的時(shí)間
- UIKeyboardAnimationCurveUserInfoKey // 鍵盤(pán)動(dòng)畫(huà)的執(zhí)行節(jié)奏(快慢)
-
通知和代理的選擇
-
共同點(diǎn)
- 利用通知和代理都能完成對(duì)象之間的通信
(比如A對(duì)象告訴D對(duì)象發(fā)生了什么事情, A對(duì)象傳遞數(shù)據(jù)給D對(duì)象)
- 利用通知和代理都能完成對(duì)象之間的通信
-
不同點(diǎn)
- 代理 : 1個(gè)對(duì)象只能告訴另1個(gè)對(duì)象發(fā)生了什么事情
- 通知 : 1個(gè)對(duì)象能告訴N個(gè)對(duì)象發(fā)生了什么事情, 1個(gè)對(duì)象能得知 N個(gè)對(duì)象發(fā)生了什么事情
-
- 總結(jié)
- 子類(lèi)指向父類(lèi),需要強(qiáng)轉(zhuǎn)一下
- 擋在最前面的控件先處理事件夫否。
- 給某個(gè)類(lèi)使用kvo監(jiān)聽(tīng)彻犁,運(yùn)行過(guò)程中系統(tǒng)默認(rèn)會(huì)給他增加一個(gè)子類(lèi),這樣會(huì)增加性能消耗凰慈,isa指向真實(shí)類(lèi) [wine valueForKeyPath:”isa” ];或[wine class]可以獲取對(duì)象的類(lèi)名 汞幢,使用kvo方法valueForKeyPath可以獲取私有屬性的值
- 代理可以傳遞事件和數(shù)據(jù),目的是解耦