iOS_UI_10_UITableView

第十章 UITableView

一、UITableView的概念
1.UITableView繼承于UIScrollView,可以滾動
2.UITableView的每一條數(shù)據(jù)對應的單元格叫做cell,是UITableViewCell的一個對象,繼承于UIView。
3.UITableView可以分區(qū)顯示,每個分區(qū)稱為section,每一行稱為row,編號都從0開始,代表一個UITableViewCell在UITableView上的位置
4.系統(tǒng)提供了一個專門的類來整合section和row,叫做NSIndexPath
二、UITableView的基本使用
1.創(chuàng)建:一種是在ViewController中alloc和initWithFrame(self.view.bounds),一種是創(chuàng)建一個類繼承于UITableViewController(不需要創(chuàng)建對象直接使用self.tableBar調用,不需要指定代理,只實現(xiàn)協(xié)議方法即可)
2.UITableView的初始化方法包含一個UITableViewStyle類型的參數(shù)(枚舉類型)
      UITableViewStylePlain---默認
      UITableViewStyleGrouped
3.UITableView顯示的相關屬性
      rowHeight(行高)
      separatorStyle(分割線樣式)
      separatorColor(分割線顏色)
      tableHeaderView(UITableView的置頂視圖)
      tableFooterView(UITableView的置底視圖)
三、UITableView顯示數(shù)據(jù)
1.UITableView的兩個重要屬性(協(xié)議代理相關)
      dataSource(顯示數(shù)據(jù)相關的代理)
      delegate(視圖操作相關的代理)
2.協(xié)議中兩個必須實現(xiàn)的協(xié)議方法
    UITableView每個分區(qū)包含的行數(shù)
         - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
    每一行要顯示的cell
         - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
3.UITableVIewCell默認的3個視圖屬性
         UIImageView* imageView----》圖片視圖
         UILabel* textLabel----》標題視圖
         UILabel* detailTextLabel----》副標題視圖
         UITableViewCellAccessoryType* accessoryType---->設置右側輔助視圖的樣式
            UITableViewCellAccessoryNone----》沒有輔助視圖
            UITableViewCellAccessoryDisclosureIndicator----》箭頭
            UITableViewCellAccessoryDetailDisclosureButton----》詳情按鈕加箭頭-
            UITableViewCellAccessoryCheckmark---->對號
            UITableViewCellAccessoryDetailButton----》詳情按鈕
          accessoryView----》當系統(tǒng)提供的輔助樣式不能滿足你,你就可以自定義視圖
四、UITableViewcell的重用機制
1.重用機制的流程
    1.當一個cell被滑出屏幕,這個cell會被系統(tǒng)放到相應的重用池中
    2.當tableView需要顯示一個cell,就會先去重用池中嘗試獲取一個cell。
    3.如果重用池沒有cell,就會創(chuàng)建一個cell
    4.取得cell之后會重新賦值進行使用
2.重用cell的代碼流程
    1.注冊cell類----iOS 6.0 以后
        - (void)registerClass:(Class)cellClass forCellReuseldentifier:(NSString*)identifier;
    2.獲取重用池中的cell的方法
        - (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier---->需要提供一個重用標識
    3.應用:
        注冊:[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
        獲?。篣ITableViewCell* cell = [tableView dequeuReuseableCellWithIdentifier:@"CELL"];
五、UITableView的常用協(xié)議方法
UITableViewDataSource
    1.UITableView分區(qū)個數(shù)
        - (NSInteger)numberOfSectionInTableView:(UITableView*)tableView;
    2.分區(qū)的頂部標題
        - (NSString*)tableView:(UITableView*)tableView titleHeaderInSection:(NSInteger)section;
    3.分區(qū)的底部標題
        - (NSString*)tableView:(UITableView*)tablrView titleFooterInSection:(NSInteger)section;
    4.UITableVie右側的索引目錄
        - (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView;
UITableViewDelegate
    1.告訴delegate選中了一個cell
        - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
    2.每一行的高度
        - (CGFloat)tableView:(UITableView*)tableView heightForRowAtInxPath:(NSIndexPath*)indexPath;
    3.每一個分區(qū)的頂部高度
        - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSIntger)section;
    4.每一個分區(qū)的頂部自定義視圖
        - (UIView*)tableView:(UITableView*)tableView viewForJeaderInSection:(NSIntger)section;
    5.cell的觸摸事件,參數(shù):indexPath:被點擊cell所在的位置(那個分區(qū)的哪一行)
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
注意:
1.tableView所呈現(xiàn)的內容都是來自于cell,我們想要將呈現(xiàn)在cell上,相當于我們想將M(數(shù)據(jù))顯示在V(tableView)上面,我們就需要C來做橋梁;這里面就涉及到了C和V之間的通信。一般C和V之間的通信,系統(tǒng)習慣使用協(xié)議代理模式
2.自己創(chuàng)建單元格的時候可以設置單元格樣式(initWithStyle)
     樣式:UITableViewCellStyleDefault----》一個imageView和一個label
           UITableViewCellStyleValue1
           UITableViewCellStyleValue2----》系統(tǒng)不提供imageView,textLabel.text:的字體顏色默認為藍色
           UITableViewCellStyleSubTitle
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容