TableView相關(guān)

一. 基礎(chǔ)屬性

  1. tableView style 只能在初始化中設(shè)置

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

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

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

    @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
    區(qū)別 :backgroundView 是處于TableView父視圖與cell層之間的一層View,在滑動(dòng)時(shí)顯示出來(lái)的就是backgroundView,而backgroundColor是整個(gè)tableView的顏色
    而backgroundColor會(huì)影響分割線的顏色,backgroundView只會(huì)在tableView滑到極限的時(shí)候能看到。另外,backgroundView只有設(shè)置一個(gè)View的時(shí)候,設(shè)置顏色才會(huì)生效(筆者嘗試添加一個(gè) ImageView 顯示圖片,并不能顯示)。

二.進(jìn)階屬性與方法

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

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

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

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

    • 返回指定位置NSIndexPath

      // 給定cell 返回NSIndexPath
       - (NSIndexPath * nullable)indexPathForCell:(UITableViewCell * nonnull)cell
      // 返回給定坐標(biāo)處的cell,此坐標(biāo)是相對(duì)于整個(gè)tableView,而不是當(dāng)前屏幕
       - (NSIndexPath * nullable)indexPathForRowAtPoint:(CGPoint)point 
      // 與上面一個(gè)類似,給一個(gè)范圍,返回指定范圍內(nèi)所有cell的NSIndexPath 數(shù)組
      // (當(dāng)tableView類型為Plain時(shí) 因?yàn)橛蟹謪^(qū),所以數(shù)組內(nèi)將包括下一分區(qū)聶內(nèi)容)
       - (NSArray<NSIndexPath *> * nullable)indexPathsForRowsInRect:(CGRect)rect
      // 返回當(dāng)前屏幕所有cell 的NSIndexPath
      @property(nonatomic, readonly) NSArray<NSIndexPath *> indexPathsForVisibleRows
      
  5. 返回cell本身

      // 根據(jù)NSIndexPath 返回對(duì)應(yīng)cell
      - (UITableViewCell * nullable)cellForRowAtIndexPath:(NSIndexPath * nonnull)indexPath
      // 返回當(dāng)前屏幕所有cell
      @property(nonatomic, readonly) NSArray<__kindof UITableViewCell *> visibleCells
    
  • 滾動(dòng)到指定位置(Scrolling the Table View)
    跳轉(zhuǎn)到指定的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. 模糊計(jì)算cell高度
    此方法在不同iOS版本上功效不一樣,模糊計(jì)算效果很差。cell自適應(yīng)高度一直是很讓人困擾的地方,此處貼出CSDN中比較好的解決方法(具體內(nèi)容待筆者研究后在分享) http://www.csdn.net/article/2015-05-19/2824709-cell-height-calculation

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

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

  2. 返回當(dāng)前選中cell的NSIndexPath

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

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

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

    // 當(dāng)tableview需要同時(shí)執(zhí)行多個(gè)動(dòng)畫時(shí),才會(huì)用到beginUpdates函數(shù),它的本質(zhì)就是建立了CATransaction這個(gè)事務(wù)。
    // 如果你僅僅更改了UITableView的cell的樣式,那么應(yīng)該試試能否通過(guò)調(diào)用beginUpdates 和 reloadRowsAtIndexPaths 
    // 來(lái)實(shí)現(xiàn)效果,而不是調(diào)用tableview的reloadData方法去重新加載全部的cell
    - (void)beginUpdates
    - (void)endUpdates
    // CELL的增加、刪除和移動(dòng)
    - (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  
    // 分區(qū)頭的增加、刪除和移動(dòng)
    - (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)
    // 刷新 整個(gè)tableView
    - (void)reloadData
    // 刷新某一個(gè)Cell
    - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> * nonnull)indexPaths
    withRowAnimation:(UITableViewRowAnimation)animation
    // 刷新某一個(gè)分區(qū)
    - (void)reloadSections:(NSIndexSet * nonnull)sections withRowAnimation:(UITableViewRowAnimation)animation
    // 刷新分區(qū)的標(biāo)題
    - (void)reloadSectionIndexTitles

  • 索引相關(guān)(Configuring the Table Index)
    // 當(dāng)設(shè)置的數(shù)值大于cell的個(gè)數(shù)則不顯示索引
    @property(nonatomic) NSInteger sectionIndexMinimumDisplayRowCount
    // 索引顏色
    @property(nonatomic, retain) UIColor *sectionIndexColor
    // 索引的背景色
    @property(nonatomic, retain) UIColor *sectionIndexBackgroundColor
    // 點(diǎn)擊索引時(shí)的顏色
    @property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,182評(píng)論 6 543
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,489評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 178,290評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 63,776評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,510評(píng)論 6 412
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 55,866評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評(píng)論 3 447
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 43,036評(píng)論 0 290
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,585評(píng)論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,331評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,536評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評(píng)論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,754評(píng)論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 35,154評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 36,469評(píng)論 1 295
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,273評(píng)論 3 399
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,505評(píng)論 2 379

推薦閱讀更多精彩內(nèi)容