ios開發 UITableViewController

來源:互聯網
上載者:User

標籤:des   style   color   使用   strong   os   

iOS中顯示資料列表最常用的一個控制項,支援垂直滾動



 
UITableView的兩種內建樣式
UITableViewStylePlain UITableViewStyleGrouped


資料來源 (dataSource)和代理 (delegate)
l? UITableView需要一個資料來源(dataSource)來顯示資料 ,UITableView會向資料來源查詢一共有多少行資料以及每一行顯 示什麼資料等。沒有設定資料來源的UITableView只是個空殼。凡 是遵守UITableViewDataSource協議的OC對象,都可以 是UITableView的資料來源
l?  通常都要為UITableView設定代理對象(delegate),以便 在UITableView觸發一下事件時做出相應的處理,比如選中了某 一行。凡是遵守了UITableViewDelegate協議的OC對象,都可 以是UITableView的代理對象 
l?  一般會讓控制器充當UITableView的dataSource和delegate 

 
   
  UITableViewDataSource

l?  @required
u? -(NSInteger) tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section;
第section分區一共有多少行
u? -(UITableViewCell*) tableView:(UITableView*)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath;
建立第section分區第row行的UITableViewCell對象(indexPath包含 了section和row)
l?  @optional
u? -(NSInteger) numberOfSectionsInTableView:(UITableView
*)tableView; 一共有多少個分區
u? -(NSString*) tableView:(UITableView*)tableView
titleForHeaderInSection:(NSInteger)section; 第section分區的頭部標題

  UITableViewDataSource
u? -(NSString*) tableView:(UITableView*)tableView  titleForFooterInSection:(NSInteger)section;
第section分區的底部標題
u? -(BOOL) tableView:(UITableView*)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以編輯(刪除)
u? -(BOOL) tableView:(UITableView*)tableView  canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
某一行是否可以移動來進行重新排序
u? -(NSArray*) sectionIndexTitlesForTableView:(UITableView *)tableView;
UITableView右邊的索引欄的內容

  UITableViewDelegate
l? - (void) tableView:(UITableView *)tableView  didSelectRowAtIndexPath:(NSIndexPath *)indexPath
選中了UITableView的某一行
l? - (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath 某一行的高度
l? - (CGFloat) tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section
第section分區頭部的高度
l? - (CGFloat) tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section 第section分區尾部的高度

  UITableViewDelegate
l? - (UIView *) tableView:(UITableView *)tableView  viewForHeaderInSection:(NSInteger)section
第section分區頭部顯示的視圖
l? - (UIView *) tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section
第section分區尾部顯示的視圖
l? - (NSInteger) tableView:(UITableView *)tableView  indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
設定每一行的等級縮排(數字越小,等級越高)

  UITableViewCell
l?  UITableView的每一行都是一個UITableViewCell,通過dataSource 的 tableView: cellForRowAtIndexPath:方法來初始化每一行 
l?  UITableViewCell是UIView的子類,內部有個預設的子視圖: contentView 。contentView是UITableViewCell所顯示內容的父視圖,並負責顯示一些 輔助指示視圖。輔助指示視圖的作用是顯示一個表示動作的表徵圖,可以通過設 置UITableViewCell的 accessoryType來顯示,預設 是UITableViewCellAccessoryNone(不顯示輔助指示視圖),其他值如下: 
u?  UITableViewCellAccessoryDisclosureIndicator  
u?  UITableViewCellAccessoryDetailDisclosureButton   u?  UITableViewCellAccessoryCheckmark 

 

 

UITableViewCellcontentView
l? contentView下預設有3個子視圖,其中的2個是UILabel(通 過UITableViewCell的textLabel和detailTextLabel屬性訪問),第3 個是UIImageView(通過UITableViewCell的imageView屬性訪問)
l? UITableViewCell還有一個UITableViewCellStyle屬性,用於決定使 用contentView的哪些子視圖,以及這些子視圖在contentView中的位置
   UITableViewCellStyleDefault
   UITableViewCellStyleSubtitle
   UITableViewCellStyleValue1
   UITableViewCellStyleValue2

 

 
UITableViewCell對象的重用原理
l? iOS裝置的記憶體有限,如果用UITableView顯示成千上萬條資料,就需 要成千上萬個UITableViewCell對象的話,那將會耗盡iOS裝置的 記憶體。要解決該問題,需要重用UITableViewCell對象
l? 重用原理:當滾動列表時,部分UITableViewCell會移出視窗 ,UITableView會將視窗外的UITableViewCell放入一個對象池中 ,等待重用。當UITableView要求dataSource返 回UITableViewCell時,dataSource會先查看這個對象池,如果池 中有未使用的UITableViewCell,dataSource會用新的資料配置這 個UITableViewCell,然後返回給UITableView,重新顯示到窗 口中,從而避免建立新對象

UITableViewCell對象的重用原理
l?  還有一個非常重要的問題:有時候需要自訂UITableViewCell(用一個子類 繼承UITableViewCell),而且每一行用的不一定是同一 種UITableViewCell(如簡訊聊天布局),所以一個UITableView可能擁有不 同類型的UITableViewCell,對象池中也會有很多不同類型 的UITableViewCell,那麼UITableView在重用UITableViewCell時可能 會得到錯誤類型的UITableViewCell 
l?  解決方案:UITableViewCell有個NSString *reuseIdentifier屬性,可 以在初始化UITableViewCell的時候傳入一個特定的字串標識來設 置reuseIdentifier(一般用UITableViewCell的類名)。當UITableView 要求dataSource返回UITableViewCell時,先通過一個字串標識到對象池 中尋找對應類型的UITableViewCell對象,如果有,就重用,如果沒有,就傳 入這個字串標識來初始化一個UITableViewCell對象 
重用 UITableViewCell對象
- (UITableViewCell *) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
cell = [[[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier] autorelease];
}
    cell.textLabel.text = [NSString stringWithFormat:@"Text %i",
 indexPath.row];
    return cell;
}

 

UITableViewCell的常用屬性
l? 設定背景
u?  backgroundView
l? 設定被選中時的背景視圖
u?  selectedBackgroundView
l?  selectionStyle屬性可設定UITableViewCell被選中時的背景顏色: u? UITableViewCellSelectionStyleNone 沒有顏色
u? UITableViewCellSelectionStyleBlue 藍色(預設)
u? UITableViewCellSelectionStyleGray 灰色

自訂 UITableViewCell  l? 一般有兩種方式:
1 用一個xib檔案來表述UITableViewCell的內容 在這設定字串標識,以便重用
2 通過代碼往UITableViewCell的contentView中添加子視圖,在 初始化方法(比如 initinitWithStyle:reuseIdentifier:) 中添加子控制項,在 layoutSubviews方法中分配子控制項的位置和大小
 
UITableView的編輯模式
l? UITableView有個 editing屬性,設定為YES時,可以進入編輯模式。在編 輯模式下,可以管理表格中的行,比如改變行的排列順序、增加行、刪除行 ,但不能修改行的內容
l? 多種方式開啟編輯模式
u? @property(nonatomic,getter=isEditing)BOOLediting
u? -(void)setEditing:(BOOL)editinganimated:(BOOL)animated
 
刪除 UITableView的行
l? 首先要開啟編輯模式
l? 實現UITableViewDataSource的如下方法:
- (void) tableView:(UITableView *)tableView  commitEditingStyle :(UITableViewCellEditingStyle)editingStyle  forRowAtIndexPath :(NSIndexPath *)indexPath {
// 如果UITableView提交的是刪除指令
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 刪除真實資料
// [self.data removeObjectAtIndex:indexPath.row];
// 刪除UITableView中的某一行(帶動畫效果)
        [tableView deleteRowsAtIndexPaths:[NSArray
    arrayWithObject:indexPath]
    withRowAnimation:UITableViewRowAnimationLeft];
// 如果不考慮動畫效果,也可以直接[tableView reload]; }

}
移動 UITableView的行
l? 首先要開啟編輯模式
l? 實現UITableViewDataSource的如下方法(如果沒有實現此方法,將無法換行)
- (void) tableView:(UITableView *)tableView  moveRowAtIndexPath :(NSIndexPath *)sourceIndexPath  toIndexPath:(NSIndexPath
*)destinationIndexPath {
int from = sourceIndexPath.row; int to = destinationIndexPath.row; if (from == to) return;
// 交換資料
   // [self.data exchangeObjectAtIndex:from
 withObjectAtIndex:to];
}

選中 UITableView的行
l? 當某行被選中時會調用此方法(UITableViewDelegate的方法)
- (void) tableView:(UITableView *)tableView  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//取消選中某一行,讓被選中行的醒目提示消失(帶動畫效果) [tableView deselectRowAtIndexPath:indexPath
 animated:YES];
}
  UITableView常用方法
l? - (id) initWithFrame:(CGRect)frame  style:(UITableViewStyle)style 初始化一個UITableView,並且設定表格樣式
l?  - (void) reloadData 重新訪問資料來源,重新整理介面 
l?  - (NSInteger) numberOfSections 分區的個數 
l?  - (NSInteger) numberOfRowsInSection:(NSInteger)section 
第section分區的行數 
l? - (UITableViewCell *) cellForRowAtIndexPath:(NSIndexPath *)indexPath
通過indexPath找到對應的UITableViewCell對象
l? - (void) setEditing:(BOOL)editing  animated:(BOOL)animated
是否要開啟編輯模式
l? - (void) deselectRowAtIndexPath:(NSIndexPath *)indexPath  animated :(BOOL)animated
取消選中某一行,讓被選中行的醒目提示消失(帶動畫效果)


UITableView常用方法
l? - (id) dequeueReusableCellWithIdentifier:(NSString *)identifier
通過identifier在(緩衝)池中找到對應的UITableViewCell對象
l? - (void) deleteRowsAtIndexPaths:(NSArray *)indexPaths  withRowAnimation:(UITableViewRowAnimation)animation
移除indexPaths範圍內的所有行
l?  @property(nonatomic,readonly)  UITableViewStyle style 表格樣式 
l?  @property(nonatomic,assign)  id <UITableViewDataSource> dataSource 資料來源 
l?  @property(nonatomic,assign)  id <UITableViewDelegate> delegate 代理 
l?  @property(nonatomic,getter= isEditingBOOL editing 是否為編輯模式 
l?  @property(nonatomic)  UITableViewCellSeparatorStyle separatorStyle 設定分隔線的樣式 
l? @property(nonatomic,retain)  UIColor *separatorColor 設定分隔線的顏色

 


UITableView常用方法
l? @property(nonatomic,retain)  UIView *tableHeaderView 表頭顯示的視圖
l? @property(nonatomic,retain)  UIView *tableFooterView 表尾顯示的視圖
l? @property(nonatomic)  BOOL allowsSelection
是否允許選中行
l? @property(nonatomic)  BOOL allowsSelectionDuringEditing 是否允許在編輯模式下選中行
l? @property(nonatomic)  BOOL allowsMultipleSelection 是否允許選中多行
l? @property(nonatomic)  BOOL allowsMultipleSelectionDuringEditing 是否允許在編輯模式下選中多行

 UITableViewController
l? 是UIViewController的子類,UITableViewController預設扮演了3種角色:視 圖控制器、UITableView的資料來源和代理
l? UITableViewController的view是個UITablView,由UITableViewController 負責設定和顯示這個對象。UITableViewController對象被建立後,會將這 個UITableView對象的dataSource和delegate指向UITableViewController自己

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.