UITableView
顯示大型內容的列表
單行,多列
垂直滾動,沒有水平滾動
大量的數據集
性能強大
TableView Styles
UITableView有兩個默認的內置風格,第一個是UITableViewStylePlain(簡明風格),第二種風格是UITableViewStyleGrouped風格(組團風格),
表格視圖以及組成部分
在頂部,有表格header,它是UITableView上的一個可設置的屬性,它只是個UIView,你能創建任何UIView子類或用UIImageView或UILable分配並設置它們爲表格視圖的tableheader view ,然後表格視圖會安置在其他其餘的內容之上,會隨着內容上下滾動。
在表格視圖的內容底部,有表格footer,與header一樣是UIView的任意子視圖,它是你設置的UITableView的屬性,然後表格視圖會安置在你其餘的內容之下,在表格header和footer視圖之間的是章節內容。
在章節中,有章節的header和章節的footer,在它們之間是所有的獨立單元,就是Tablecell。
使用TableViews
在表格視圖中顯示數據
定製顯示的外觀和行爲
解決方法
表格視圖用來顯示一列數據,所以使用一個數組,可以用[myTableView setList : myListOfStuff];
但該方法爭議性
所有的數據必須事先裝載
所有的數據都裝載在內存中
其他方法
另外一個對象爲表格視圖提供數據
不是一次性全部取得
只取現在需要展示的數據
像一個委託,它是純粹的數據導向,它只向數據源詢問數據
UITableViewDataSource(數據源協議)
提供章節和行的數目
//optionalmethod ,default to 1 if not implemented
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
//requiredmethod
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
必要的時候向表格視圖提供所要顯示的cells
//required method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;//在該方法中,UITableViewCell是UIView的一個子類,它知道如何在表格視圖滾軸,知道如何繪製分組風格表格視圖和組羣風格表格視圖,它必須自己是一個UITableViewCell或者UITableViewCell的子類。
NSIndexPath
是Foundation框架中的一個普通的類,它提供了到嵌套數列的樹中特定節點的路徑,事實上,它是一個整數陣列,表格視圖使用這個去表現在特定章節中的特定行,UITableView用的所有索引路徑正好有兩個元素,第一個是章節,第二個是行。
NSIndexPath和TableViews
@interfaceNSIndexPath (UITableView) {
}
+(NSIndexPath*)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
@property(nonatomic,readonly)NSUIntegerrow;
@property(nonatomic,readonly)NSUIntegersection;
@end
SingleSection Table View
返回行數
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return[myStringcount];
}
請求時提供一個單元
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell = ......;
cell.textLabel.text=[myStringobjectAtIndex:indexPath.row];
return[cell autorelease];
}
單元複用(cellreuse)
輕滑屏幕,你必須一次滾動上百行的文字,很多很多的不同單元,不能完成的很好,每次一個消失,你破壞呢個表格視圖單元,然後爲下一個創建新的,我們最終會做的是當一個表格視圖單元從頂部消失,它會放在一個複用隊列中,並且在你的TableViewCellforRowIndexPath方法中,你可以選擇,當然你應該總是這樣做,你有機會去在分配新的單元前,向表格視圖詢問複用隊列其中的一個單元,如果一個單元已經從頂部滾軸消失,對於顯示當前可視的東西,它不會必須的,你能取得它改變它的內容爲你所需要顯示的新行而它會被重新滾進底部。(有點像循環瀑布)
-(UITableViewCell*)dequeueReusableCellWithindentifier:(NSString*)identifier;
//取回可複用的一個單元
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"MyIdentifier"];
if(cell== nil){
cell=[[[UITableViewCell alloc] initWithStyle:.....reuseIdentifier:@"MyIdentifier"]autorelease];
}
cell.text= [myStringobjectAtIndex:indexPath.row];
returncell;
}
觸發一個更新
什麼時候數據源會被請求數據
當一行變得可視
完全更新會被響應當使用reloadData
-(void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated]; //所有的數據重新被加載,然後在放到複用隊列中
[self.tableViewreloadData];
}
章節和行進行重載數據
-(void)insertSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以插入整個章節
-(void)ideleteSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以刪除整個章節
-(void)reloadSections:(NSIndexSet*)sections withRowAnimation:
(UITableViewRowAniamtion)animation;//在iPhoneos 3.0中重載章節數據(上面兩個方法的合併)
它們能重載部分的數據,而不會把所有東西都丟掉
-(void)insertRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;
-(void)deleteRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;
-(void)reloadRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;
其它的數據源方法
爲章節的header和footer設置標題(表格的header和footer是表格視圖上的屬性,你只分配那些視圖章節header和footer是依據章節的,因此它們由委派功能所提供)
允許編輯和重排單元
外觀和行爲
UITableViewDelegate
可定製外觀和行爲
邏輯獨立於視圖
數據源和委派經常是相同的對象(事實上,很少情況下數據源不是委派)
表格視圖的外觀和行爲
定製表格視圖單元的外觀
-(void)tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath;
這個會在表格視圖的其中一個單元變爲可視前立刻被調用,它是你在表格視圖單元顯示在屏幕上之前的外觀定製的最後機會,它保證一旦你調用這個方法,我們不會涉及任何單元的外觀,這之後,你改變的任何東西都會顯示在屏幕上,如果有一些東西你可能在TableViewcellForRowAtIndexPath方法中不知道,你想稍微懶惰地做這個,這會在那之後最後的機會去修改外觀。
驗證和響應所選擇的變化
使用表格視圖委派功能,對於選擇會有不同的功能
-(NSIndexPath*)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;//用戶選擇完成擡起手指,在該方法中,你可能決定去分配新的UIViewController,然後推它到self.navigationContrller,因此一些新事物滑進來,行選擇通常會觸發一個事件。選擇通常不是一個持續的事情,它通常不會在你輕敲之後保持選擇狀態,通常會談出選擇然後完成一個行爲,或者你把它滑出屏幕,當然滑回來時會淡出。在iphone應用程序中你也會看到連續的選擇,我們在表格試圖中是支持持續選擇的,如果用戶選擇一行而你沒有反選它,我們不會替你反選,過去在iPhoneOS 2.0中不是這樣的,如果你不反選它,這個行爲是不確定的,我們現在支持這個持續選擇行爲。
響應選擇
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
NSIntegerrow = indexPath.row ;
idobjectToDispaly = [myObjects ObjectAtIndex:row ];
MyViewController* myViewController = …....;
myViewController.object = objectToDispaly ;
[self.navigationControllerpushViewController: myViewController animated:YES];
}
修改和禁用選擇
-(NSIndexPath*)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row== ….) {
returnnil;
}
else{ return indexPath ;}
}
UITableViewController
是UIViewController的子類,如果你想用全屏表格試圖風格,它會是很便利的起點,表格試圖會被自動創建,這包括控制器,表格視圖,數據源和委派,它在你第一次出現時替你調用reloadData,它在用戶導航回去時反選,會知道你之前所選的item。UITableViewController在正確的時間替你處理反選的事情,它還會閃爍滾動指示條,實際上這是iPhone人機界面的一部分。
TableView cells
指定的初始化程序
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier ;
//舊的版本,現在已經不是默認的初始化程序了
-(id)initWithStyle:(UITableviewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier ;
//新版本
基本屬性
UITableViewCell有一個圖像視圖和一到兩個的文本標籤
cell.imageView.image= [UIImage imageNamed:@”vitolidok.png”];
cell.textLabel.text= @”Vitol Idol”;
cell.detailTextLabel.text=@”Billy Idol”;
如果在表格視圖單元的默認風格上設置detailTextLabel.text,它是不會做任何事情,因爲它不顯示一個額外的detailText。
附件類型
//UITableView委託方法
-(UITableViewCellAccessoryType)tableView:(UITableView*)table accessoryTypeForRowWithIndexPath:(NSIndexPath *) indexPath;
-(void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *) indexPath {
//只有是在藍色描述的按鈕(也就是上面的第二個)
NSIntegerrow = indexPath.row.
}
定製內容視圖
在有些情況下,簡單的圖像和文本是不夠的
UITableViewCell有一個contentview 屬性
爲contentview 添加額外的視圖
-(UITableViewCell*) tableView :(UITableView) tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell*cell = ….......;
CGRectframe = cell.contentView.bounds;
UILabel*myLabel = [[UILabel alloc] initWithFrame :frame];
myLabel.text= ….;
[cell.contentViewaddSubview : myLabel];
[myLabelrelease];
}