TableView相關

一. 基礎屬性

  1. tableView style 只能在初始化中設置

      @property(nonatomic, readonly) UITableViewStyle style 
    
      typedef NS_ENUM(NSInteger, UITableViewStyle) {
      UITableViewStylePlain,          // regular table view
      UITableViewStyleGrouped         // preferences style table view
    };
    
  2. 設置分割線style(枚舉):

     @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
    
     typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
         UITableViewCellSeparatorStyleNone, // 不顯示分割線
         UITableViewCellSeparatorStyleSingleLine, // 一條風分割線
         UITableViewCellSeparatorStyleSingleLineEtched // 與None 看起來相同 = = 
     } __TVOS_PROHIBITED;
    
  3. 設置分割線顏色

     @property(nonatomic, retain) UIColor *separatorColor
    
  4. 設置分割線效果(IOS8之后可用) 具體實現效果未發現,有待考證

    @property(nonatomic, copy) UIVisualEffect *separatorEffect
    ableView.separatorEffect = [UIVibrancyEffect effectForBlurEffect:
    [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    //枚舉
    typedef NS_ENUM(NSInteger, UIBlurEffectStyle) {
        UIBlurEffectStyleExtraLight,
        UIBlurEffectStyleLight,
        UIBlurEffectStyleDark
    } NS_ENUM_AVAILABLE_IOS(8_0);
    
  5. tableView backgroundView 與 backgroundColor
    區別 :backgroundView 是處于TableView父視圖與cell層之間的一層View,在滑動時顯示出來的就是backgroundView,而backgroundColor是整個tableView的顏色
    而backgroundColor會影響分割線的顏色,backgroundView只會在tableView滑到極限的時候能看到。另外,backgroundView只有設置一個View的時候,設置顏色才會生效(筆者嘗試添加一個 ImageView 顯示圖片,并不能顯示)。

二.進階屬性與方法

  1. 頭視圖與尾視圖 (Accessing Header and Footer Views)
  • tableView頭尾視圖(注:不同于Session的頭尾視圖)
    需要實例化一個UIView對象,頭尾視圖指針指向這個UIView對象,與Session的重用機制不同,tableView的頭尾不需要考慮重用池,一般建議輪播圖寫在此處
    @property(nonatomic, retain) UIView *tableHeaderView
    @property(nonatomic, retain) UIView *tableFooterView
  1. Session 頭尾高度
    雖然此方法可以設置分區的頭尾高度,但是不建議使用,建議使用代理方法設置(詳見下文)。使用此方法有可能第一個分區的高度還是系統默認高度
    @property(nonatomic) CGFloat sectionHeaderHeight
    @property(nonatomic) CGFloat sectionFooterHeight

  2. Session 頭尾注冊與使用
    // 注冊
    - (void)registerClass:(Class nullable)aClass forHeaderFooterViewReuseIdentifier:
    (NSString * nonnull)identifier
    // 當使用xib創建視圖的時候 使用此方法 需要初始化UINib?
    - (void)registerNib:(UINib * nullable)nib forHeaderFooterViewReuseIdentifier:
    (NSString * nonnull)identifier
    // 從重用池調用
    - (_kindofUITableViewHeaderFooterView * nullable)
    dequeueReusableHeaderFooterViewWithIdentifier:(NSString * nonnull)identifier

  3. 獲取指定Session頭尾視圖
    // section 為指定分區
    - (UITableViewHeaderFooterView * nullable)headerViewForSection:(NSInteger)section

     - (UITableViewHeaderFooterView * nullable)footerViewForSection:(NSInteger)section
    
  4. 返回cell位置相關(Accessing Cells and Sections)

    • 返回指定位置NSIndexPath

      // 給定cell 返回NSIndexPath
       - (NSIndexPath * nullable)indexPathForCell:(UITableViewCell * nonnull)cell
      // 返回給定坐標處的cell,此坐標是相對于整個tableView,而不是當前屏幕
       - (NSIndexPath * nullable)indexPathForRowAtPoint:(CGPoint)point 
      // 與上面一個類似,給一個范圍,返回指定范圍內所有cell的NSIndexPath 數組
      // (當tableView類型為Plain時 因為有分區,所以數組內將包括下一分區聶內容)
       - (NSArray<NSIndexPath *> * nullable)indexPathsForRowsInRect:(CGRect)rect
      // 返回當前屏幕所有cell 的NSIndexPath
      @property(nonatomic, readonly) NSArray<NSIndexPath *> indexPathsForVisibleRows
      
  5. 返回cell本身

      // 根據NSIndexPath 返回對應cell
      - (UITableViewCell * nullable)cellForRowAtIndexPath:(NSIndexPath * nonnull)indexPath
      // 返回當前屏幕所有cell
      @property(nonatomic, readonly) NSArray<__kindof UITableViewCell *> visibleCells
    
  • 滾動到指定位置(Scrolling the Table View)
    跳轉到指定的Index

      - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:
      (UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
      //  其中 scrollPosition 為枚舉值 
      typedef enum {
         UITableViewScrollPositionNone,
         UITableViewScrollPositionTop,
         UITableViewScrollPositionMiddle,
         UITableViewScrollPositionBottom 
      } UITableViewScrollPosition;
      // 其中indexPath 為tableViewCell 的位置 可自己定義
      NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    
  1. 模糊計算cell高度
    此方法在不同iOS版本上功效不一樣,模糊計算效果很差。cell自適應高度一直是很讓人困擾的地方,此處貼出CSDN中比較好的解決方法(具體內容待筆者研究后在分享) http://www.csdn.net/article/2015-05-19/2824709-cell-height-calculation

     @property(nonatomic) CGFloat estimatedRowHeight
    
  2. 選擇cell的相關設置(Inserting, Deleting, and Moving Rows and Sections)

  • 是否允許選擇Cell (編輯狀態的設置下文會提到)
    @property(nonatomic) BOOL allowsSelection // 非編輯狀態
    @property(nonatomic) BOOL allowsSelectionDuringEditing// 編輯狀態
  1. 是否允許選擇多個行進行編輯
    @property(nonatomic) BOOL allowsMultipleSelection // 非編輯狀態是否允許被選中
    @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing //編輯狀態是否允許被選
    默認為NO, 當設置為YES的時候,若沒有去除點擊的灰色效果會發現可以有多個Cell變成點擊的樣子,再次點擊取消,此過程會調用回調方法。反之,只能有一個cell為點擊狀態
    配合回調方法使用()
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

  2. 返回當前選中cell的NSIndexPath

      // 返回當前選中的cell的IndexPath
      - (NSIndexPath *)indexPathForSelectedRow
      // 返回當前選中的cell的IndexPath,此方法返回值為一個數組 。
      // 使用在allowsMultipleSelection = YES 的時候
      - (NSArray *)indexPathsForSelectedRows
    
  3. 設置cell選中和取消選中狀態
    - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated: (BOOL)animated
    scrollPosition:(UITableViewScrollPosition)scrollPosition
    // UITableViewScrollPosition Cell選中后的位置
    - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath
    animated:(BOOL)animated

  • Cell的編輯狀態設置(Managing the Editing of Table Cells)
    //當要對cell編輯的時候,要將此屬性設為YES,此屬性不可以直接賦值,需要調用下面的set方法
    @property(nonatomic, getter=isEditing) BOOL editing

    - (void)setEditing:(BOOL)editing animated:(BOOL)animate
    
  • 編輯(Inserting, Deleting, and Moving Rows and Sections)

    // 當tableview需要同時執行多個動畫時,才會用到beginUpdates函數,它的本質就是建立了CATransaction這個事務。
    // 如果你僅僅更改了UITableView的cell的樣式,那么應該試試能否通過調用beginUpdates 和 reloadRowsAtIndexPaths 
    // 來實現效果,而不是調用tableview的reloadData方法去重新加載全部的cell
    - (void)beginUpdates
    - (void)endUpdates
    // CELL的增加、刪除和移動
    - (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths 
            withRowAnimation:(UITableViewRowAnimation)animation
    - (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths 
            withRowAnimation:(UITableViewRowAnimation)animation
    - (void)moveRowAtIndexPath:(NSIndexPath * nonnull)indexPath*toIndexPath:(NSIndexPath * nonnull)newIndexPath  
    // 分區頭的增加、刪除和移動
    - (void)insertSections:(NSIndexSet * nonnull)sections*withRowAnimation:(UITableViewRowAnimation)animation
    - (void)deleteSections:(NSIndexSet * nonnull)sections*withRowAnimation:(UITableViewRowAnimation)animation
    - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
    
  • 刷新TableView (Reloading the Table View)
    // 刷新 整個tableView
    - (void)reloadData
    // 刷新某一個Cell
    - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths
    withRowAnimation:(UITableViewRowAnimation)animation
    // 刷新某一個分區
    - (void)reloadSections:(NSIndexSet * nonnull)sections withRowAnimation:(UITableViewRowAnimation)animation
    // 刷新分區的標題
    - (void)reloadSectionIndexTitles

  • 索引相關(Configuring the Table Index)
    // 當設置的數值大于cell的個數則不顯示索引
    @property(nonatomic) NSInteger sectionIndexMinimumDisplayRowCount
    // 索引顏色
    @property(nonatomic, retain) UIColor *sectionIndexColor
    // 索引的背景色
    @property(nonatomic, retain) UIColor *sectionIndexBackgroundColor
    // 點擊索引時的顏色
    @property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容