UIKit-UITableView詳解


UITableView

一.UITableView

<p>tableView 是項目開發(fā)里經常用到的控件埋嵌,剛開始使用時可能會覺得這玩意功能強大但實現起來復雜,不過用多了俱恶,就會覺得它功能強大而且簡單雹嗦。
<p>分類 tableView有兩種風格范舀,每次初始化時必須制定一種風格,默認是 Plain了罪,而且創(chuàng)建成功后是不可更改的锭环,所以希望通過一個tableView實例在兩種風格之間互相切換的少年實在是想多了。
UITableViewStylePlain:section 的headers和footers是漂浮在內容上層的捶惜,而且這種風格的列表可以在右側有一個索引條田藐,通過索引直接跳轉到對應的section。
UITableViewStyleGrouped:有默認的background color和background view 吱七,iOS7之前每個分組都有圓角效果感覺挺叼,不過iOS7扁平化之后跟UITableViewStylePlain也沒啥區(qū)別了鹤竭,而且不能使用索引功能踊餐。
<p>創(chuàng)建 tableView必須有一個協(xié)議的實現類和數據源的實現類。

    // 1.初始化plain風格的table臀稚,plain可以使用索引功能
    self.plainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 80, self.view.bounds.size.width, self.view.bounds.size.height - 80)];
    self.plainTableView.dataSource = self;//  設置數據源
    self.plainTableView.delegate = self;//  設置代理

    //2.實現數據源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return 10;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *identifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
    }

<p>屬性 tableView通過屬性設置各項數值

    self.plainTableView.tag = 10001;
    
    self.plainTableView.rowHeight = 92;//   單元格高度吝岭,優(yōu)先級比                               -tableView:heightForRowAtIndexPath:低
    self.plainTableView.sectionHeaderHeight = 40;
    self.plainTableView.sectionFooterHeight = 40;
    
    self.plainTableView.estimatedRowHeight = 92;//  估算單元格高度,(iOS7)
    self.plainTableView.estimatedSectionHeaderHeight = 40;//  估算section頭部視圖高度吧寺,(iOS7)
    self.plainTableView.estimatedSectionFooterHeight = 40;//  估算section尾部視圖高度窜管,(iOS7)
    
    self.plainTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 20);//   分割線間隙,(iOS7)
    
    self.plainTableView.backgroundColor = [UIColor whiteColor];//  背景色
    self.plainTableView.backgroundView = nil;//  背景視圖稚机,想要顯示背景視圖必須先把單元格的背景色設為clearColor

<p>編輯 tableView由單元格組成幕帆,每個單元格都具有選中屬性,實現了tableView的單選和多選赖条,不過tableView存在編輯模式和普通模式失乾,所以選擇也分為編輯模式下和普通模式下的單選和多選,默認只能在普通模式下單選纬乍。除此之外單元格的選中也分為多個樣式:

  • UITableViewCellSelectionStyleNone
  • UITableViewCellSelectionStyleBlue
  • UITableViewCellSelectionStyleGray
  • UITableViewCellSelectionStyleDefault (iOS7)
    self.plainTableView.allowsSelection = YES;//  默認是YES碱茁,決定單元格在非編輯模式下是否可單選
    
    self.plainTableView.allowsSelectionDuringEditing = NO;//  默認是NO,決定單元格在編輯狀態(tài)下是否可單選

    self.plainTableView.allowsMultipleSelection = NO;//  默認是NO仿贬,決定單元格在非編輯模式下是否可多選纽竣,不被allowsSelection影響
    
    self.plainTableView.allowsMultipleSelectionDuringEditing = NO;//  默認是NO,決定單元格仔編輯狀態(tài)下是否可多選茧泪,當該值被設為YES時蜓氨,列表的編輯模式將沒有刪除、添加

<p>信息 tableView有許多方法獲取自身的各項屬性

//  獲取number
    NSInteger numOfSection = [self.plainTableView numberOfSections];
    
    NSInteger numOfRowInSection0 = [self.plainTableView numberOfRowsInSection:0];
    
    NSLog(@"%d%d",numOfSection,numOfRowInSection0);
    
    //  獲取Frame
    CGRect sectionRect = [self.plainTableView rectForSection:0];//  包括section頭部視圖和尾部視圖的
    
    CGRect headerRect = [self.plainTableView rectForHeaderInSection:0];
    
    CGRect footerRect = [self.plainTableView rectForFooterInSection:0];
    
    CGRect cellRect = [self.plainTableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    NSLog(@"%f%f%f%f",sectionRect.size.height,headerRect.size.height,footerRect.size.height,cellRect.size.height);
    
    //  獲取indexPath
    NSIndexPath *indexPath0 = [self.plainTableView indexPathForRowAtPoint:CGPointMake(10, 200)];//  當點不在列表任意單元格中的時候返回nil
    
    NSIndexPath *indexPath1 = [self.plainTableView indexPathForCell:[self.plainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]]];//   當單元格不可見的時候返回nil
    
    NSArray *indexPathArr0 = [self.plainTableView indexPathsForRowsInRect:CGRectMake(0, 0, 200, 0)];//  當Rect無效時返回nil
    
    NSArray *visibleArr = [self.plainTableView indexPathsForVisibleRows];
    
    NSArray *selectedArr = [self.plainTableView indexPathsForSelectedRows];
    
    //  獲取Cell
    UITableViewCell *cell = [self.plainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:0]];//  當單元格不可見活著indexPath超出范圍時返回nil
    
    NSArray *cellArr = [self.plainTableView visibleCells];
    
    //  獲取UITableViewHeaderFooterView
    UITableViewHeaderFooterView *headerView = [self.plainTableView headerViewForSection:0];//   (iOS6)
    
    UITableViewHeaderFooterView *footerView = [self.plainTableView footerViewForSection:0];//   (iOS6)

<p>外觀 索引樣式调炬、分割線樣式语盈、單元格樣式、列表頭部尾部等等

    self.plainTableView.sectionIndexMinimumDisplayRowCount = 1;// 當指定section的行數達到這個值的時候缰泡,在右側顯示該section的索引列表
    self.plainTableView.sectionIndexColor = [UIColor blackColor];// section索引的文字顏色刀荒,(iOS6)
    self.plainTableView.sectionIndexBackgroundColor = [UIColor grayColor];// 索引未被觸碰時的背景顏色代嗤,(iOS7)
    self.plainTableView.sectionIndexTrackingBackgroundColor = [UIColor redColor];// 索引被觸碰時的背景顏色,(iOS6)
    
    /* UITableViewCellSeparatorStyle
     * UITableViewCellSeparatorStyleNone,
     * UITableViewCellSeparatorStyleSingleLine,
     * UITableViewCellSeparatorStyleSingleLineEtched  只有group的table可以使用該樣式
     */
    self.plainTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;// 現在一般都是設為UITableViewCellSeparatorStyleNone然后自定義
    self.plainTableView.separatorColor = [UIColor purpleColor];// 默認是gray
    self.plainTableView.separatorEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];// 列表分隔的樣式效果缠借,(iOS8)need to do more
    
    /*
     * 啟用與iOS6干毅,客戶端可以為每一個cell注冊一個nib或class
     * 如果全部重用identifiers都注冊了,使用新方法-dequeueReusableCellWithIdentifier:forIndexPath:來保證一個單元格實例返回
     * 當從dequeue方法中返回實例的時候它們會重置正確的大小
     */
    [self.plainTableView registerNib:[UINib nibWithNibName:@"customCell"  bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"customCell"];//  注冊自定義單元格泼返,注冊后在單元格重制方法里才能只通過Identifier就找到對應的單元格硝逢,(iOS5)
    
    //- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
    
    //- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
    
    //- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

<p>方法

    //1.刷新數據
    - (void)reloadAction {
    [self.plainTableView reloadData];
}
    //2.刷新索引
    - (void)reloadIndexTitleAction {
    [self.plainTableView reloadSectionIndexTitles];
}
    //3.強制定位
    - (void)scrollToIndexPath {
    /* UITableViewScrollPosition的枚舉
     * UITableViewScrollPositionNone
     * UITableViewScrollPositionTop
     * UITableViewScrollPositionMiddle
     * UITableViewScrollPositionBottom
     */
    [self.plainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
    //4.定位選中行
    - (void)scrollToNearestSelectedRow {
    [self.plainTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];// 定位到indexPath最小的選中單元格的位置,多選一樣有效
}
    //5.模式變換
    - (void)edit:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.plainTableView setEditing:sender.selected animated:YES];
}
    //6.選中行并定位   
    - (void)getPlainTableViewSelection {
    NSIndexPath *indexPath = [self.plainTableView indexPathForSelectedRow];// 當有多個選中是绅喉,返回indexPath最小的那個
    
    NSArray *indexPathArr = [self.plainTableView indexPathsForSelectedRows];// (iOS5)
    
    [self.plainTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];// 選中
    
    [self.plainTableView deselectRowAtIndexPath:indexPath animated:YES];// 取消選中
} 

二.UITableView的四大功能

<p>添加渠鸽、刪除

/* 實現添加刪除功能
 * 1:實現-tableView:canEditRowAtIndexPath: ,不實現時全部cell默認可編輯柴罐,該方法決定是否可編輯
 * 2.實現-tableView:commitEditingStyle:forRowAtIndexPath:徽缚,必須實現,該方法自定義添加刪除功能
 * 3.實現-tableView:editingStyleForRowAtIndexPath:革屠,不實現時為刪除樣式凿试,該方法決定編輯樣式
 * 4.可以實現-tableView:willBeginEditingRowAtIndexPath:
            -tableView:didEndEditingRowAtIndexPath:
     兩個方法監(jiān)聽編輯動作并作相應自定義
 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // 可以不實現該方法,默認全部單元格都是可編輯似芝,也可在此處控制單元格是否可編輯
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 在這里定義添加刪除功能那婉,當添加或刪除按鈕被點擊時,datasource必須提交更改党瓮,當edit動作使用UITableViewRowAction時详炬,處理動作會被取代
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [tableData removeObjectAtIndex:indexPath.row];
        [self.plainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
     * UITableViewCellEditingStyleNone
     * UITableViewCellEditingStyleDelete
     * UITableViewCellEditingStyleInsert
     */
    return UITableViewCellEditingStyleNone;
}

<p>排序

/* 移動功能
 * 1.實現-tableView:canMoveRowAtIndexPath:,不實現時全部單元格默認可移動
 * 2.實現-tableView:moveRowAtIndexPath:toIndexPath:麻诀,必須實現痕寓,在該方法中定義移動功能,只有實現了該方法蝇闭,編輯模式下才顯示移動圖標
 * 3.可以實現
        -tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath
   自定義某些特定功能呻率,不過盡量還是別實現,因為這個方法cell在移動過程中每經過一個cell就會調用一次呻引,調用地多會影響性能
 * 4.可以實現
        -tableView:willBeginEditingRowAtIndexPath:
        -tableView:didEndEditingRowAtIndexPath:
   兩個方法監(jiān)聽編輯動作并作相應自定義
 */
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // 允許某個指定行顯示復制視圖礼仗,默認情況下只有datasource實現了-tableView:moveRowAtIndexPath:toIndexPath:才顯示
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    if (sourceIndexPath != destinationIndexPath) {
        id object = [tableData objectAtIndex:sourceIndexPath.row];
        [tableData removeObjectAtIndex:sourceIndexPath.row];
        if (destinationIndexPath.row > [tableData count]) {
            [tableData addObject:object];
        }
        else {
            [tableData insertObject:object atIndex:destinationIndexPath.row];
        }
    }
}

<p>索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    // 設置section標題的列表來顯示在section索引視圖上
    return @[@"a",@"a",@"a",@"a",@"a",@"a"];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    // 告訴列表哪個section對應哪個索引
    return index;
}

<p>復制

#pragma mark  - 復制黏貼功能
// 三個方法必須都實現,缺一不可
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 允許長按顯示菜單逻悠,也可以作為cell長按手勢回調
    NSLog(@"---------長按");
    return NO;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    // 允許每一個Action
    return  YES;
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    // 對一個給定的行告訴代表執(zhí)行復制或粘貼操作內容
    if (action==@selector(copy:)) {//如果操作為復制
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];//黏貼板
        [pasteBoard setString:cell.textLabel.text];
        NSLog(@"%@",pasteBoard.string);//獲得剪貼板的內容
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末元践,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子童谒,更是在濱河造成了極大的恐慌单旁,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饥伊,死亡現場離奇詭異象浑,居然都是意外死亡蔫饰,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門愉豺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來篓吁,“玉大人,你說我怎么就攤上這事蚪拦≌燃簦” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵驰贷,是天一觀的道長盛嘿。 經常有香客問我,道長括袒,這世上最難降的妖魔是什么孩擂? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮箱熬,結果婚禮上,老公的妹妹穿的比我還像新娘狈邑。我一直安慰自己城须,他們只是感情好,可當我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布米苹。 她就那樣靜靜地躺著糕伐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蘸嘶。 梳的紋絲不亂的頭發(fā)上良瞧,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機與錄音训唱,去河邊找鬼褥蚯。 笑死,一個胖子當著我的面吹牛况增,可吹牛的內容都是我干的赞庶。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼澳骤,長吁一口氣:“原來是場噩夢啊……” “哼歧强!你這毒婦竟也來了?” 一聲冷哼從身側響起为肮,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤摊册,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后颊艳,有當地人在樹林里發(fā)現了一具尸體茅特,經...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡忘分,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了温治。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片饭庞。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖熬荆,靈堂內的尸體忽然破棺而出舟山,到底是詐尸還是另有隱情,我是刑警寧澤卤恳,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布累盗,位于F島的核電站,受9級特大地震影響突琳,放射性物質發(fā)生泄漏若债。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一拆融、第九天 我趴在偏房一處隱蔽的房頂上張望蠢琳。 院中可真熱鬧,春花似錦镜豹、人聲如沸傲须。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泰讽。三九已至,卻和暖如春昔期,著一層夾襖步出監(jiān)牢的瞬間已卸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工硼一, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留累澡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓欠动,卻偏偏與公主長得像永乌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子具伍,可洞房花燭夜當晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件翅雏,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,061評論 3 38
  • 發(fā)現 關注 消息 iOS 第三方庫人芽、插件望几、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,109評論 4 62
  • 無論球隊是贏得勝利凱旋而歸還是輸掉比賽垂頭喪氣,俄城的球迷萤厅,無論男女老少總會在凌晨自發(fā)前往機場等候客場疲憊歸來的主...
    Kusok閱讀 113評論 0 0
  • 小時候橄抹,管理發(fā)叫剃頭靴迫。 那時也不知道什么理發(fā)師,全由本家的大爺給剃頭楼誓。也沒什么頭型玉锌,就是拿剃頭推子,推一個光頭疟羹。 ...
    懷雙閱讀 452評論 6 13
  • 這四年最美好的回憶主守,是遇見了最想照顧一生的你;這四年最長的嘆息榄融,是相遇在我還沒有能力的年紀参淫。 明明想聽你的消...
    聽水榭主人閱讀 155評論 0 0