UITableView

目錄

  • 設置協議
  • 創建cell
  • 用nib創建cell
  • UITableViewDataSource
  • UITableViewDelegate
  • 常用屬性
  • 不常用屬性
  • TableView滾動時調用的方法
  • titles索引列

UITableView 繼承 UIScrollView
//獲取選中的cell
self.myTableView.indexPathForSelectedRow.row

設置協議

1.創建tableView對象

 UITableViewStylePlain,  (默認)扁平風格(也是可以分組)
 UITableViewStyleGrouped  分組

 tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
 //2.顯示在界面上
 [self.view addSubview:tableView];
補充:UICollectionView底部被標簽了控制器(tabBar)遮擋的解決辦法
//
 self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-44) collectionViewLayout:flowLayout];
    
 self.automaticallyAdjustsScrollViewInsets = NO;
 self.collectionV.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);

3.設置代理
數據相關的代理(只有實現dataSource的協議方法,才能在tableView上去顯示數據)

//聲明協議
 @interface ViewController ()<UITableViewDataSource>
  
//設置代理
 tableView.dataSource = self;

UITableViewDataSource

必須實現的協議方法

設置每組的個數

返回值:設置分組中的行數(每組cell的個數)
參數1:委托
參數2:第幾組

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //需要每一組顯示100條數據
    return 100;
}
Cell(tableView顯示數據,也不是通過tableView本身去顯示數據,而是通過cell來顯示)

返回值:創建好的cell
參數1:委托
參數2:cell的位置(和坐標沒有關系,只和組和行有關)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  !!!!!面試過程中常問的問題:
  1.去復用池中查看是否有可以復用(重復使用)的cell;如果有就返回可以復用的cell地址,沒有返回nil
   參數:復用ID
 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    
  2.判斷是否拿到可以復用cell,如果沒有拿到就創建一個新的cell(最終創建cell的個數是整屏顯示的cell的個數加1或者加2)
    if (cell==nil) {
    參數1:風格
    參數2:復用ID

   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
    }

  3.刷新數據(刷新cell上顯示的內容)
   indexPath由section(組)和row(行)組成
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld組,第%ld行", indexPath.section, indexPath.row];
    
   4.返回cell
   return cell;
    
}

設置cell的選中樣式

//UITableViewCellSelectionStyleNone沒有選中的效果
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

nib定制Cell

讓TableView先注冊nib

 // 注冊單元格
 [_tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:SubjectCellIdentifier];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // 復用單元格
  SubjectCell * cell = [tableView dequeueReusableCellWithIdentifier:SubjectCellIdentifier forIndexPath:indexPath];
    
  // 獲取數據模型
  SubjectModel * model = self.subjectArray[indexPath.row];
    
  cell.model = model;
    
  return cell;
}

設置tableView的分組數(默認是1個分組)
參數:委托

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;  
}

可選協議

設置header的標題

- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    switch (section) {
        case 0:
            return @"熱門";
            break;
            
        case 1:
            return @"最近更新";
            break;
        
        case 2:
            return @"銷量最高";
            break;
            
        default:
            break;
    }
    return @"頭標題";
}

設置footer的標題

- ( NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return @"腳標題";
    
}

UITableViewDelegate

MARK高度相關

設置Cell行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 50; 
}
選中相關

選中一個cell的時候會調用這個方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"cell被選中");
    //在這兒跳轉到下一個界面
    NextViewController * next = [[NextViewController alloc] init];
    
    //傳值
    next.indexpath = indexPath;
    
    
    //push到下一個界面
    [self.navigationController pushViewController:next animated:YES];
    
    //present到下一個界面
    [self presentViewController:next animated:YES completion:nil];
    
}
頭視圖和腳視圖相關

設置header高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 50 * section + 10;
    return 50;
}

設置Footer高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 50;
}

設置每一組的headerView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

   //創建一個視圖(設置frame無效)
   UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
   //設置背景顏色
   headerView.backgroundColor = [UIColor redColor];
        
   //創建一個label
   UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
   label.text = [NSString stringWithFormat:@"頭標題:%ld", section];
   [headerView addSubview:label];
        
   return headerView;
    
}

設置每組腳視圖Footer

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    //創建一個視圖(設置frame無效)
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    //設置背景顏色
    headerView.backgroundColor = [UIColor yellowColor];
    
    
    //創建一個label
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
    label.text = @"腳標題";
    [headerView addSubview:label];
    
    
    return headerView;
    
}

Cell的附件相關

設置指定位置的cell的附件類型

  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    UITableViewCellAccessoryNone,(默認)
    UITableViewCellAccessoryDisclosureIndicator 一個小箭頭(常用)
    UITableViewCellAccessoryDetailDisclosureButton 一個詳情按鈕加小箭頭
    UITableViewCellAccessoryCheckmark,  一個藍色勾
    UITableViewCellAccessoryDetailButton  一個詳情按鈕
      
添加完箭頭以后,若還想在箭頭前面添加文字

http://www.th7.cn/Program/IOS/201410/289058.shtml


cell.detailTextLabel.text = @"mona";
如果你執行之后可以看到箭頭前有文字出現的話,那么恭喜你,你是幸運的。
我就沒那么幸運,改了很多次之后都cell右側都沒有顯示過文字出來,后來就根據這個情況,找了很久都沒找到相應的解決方法。
后來在一段demo中才知道,原來要在右側顯示出detailTextLabel的文字,還需要在cellForRowAtIndexPath:中把cell的類型設置為

UITableViewCellStyleValue1,cellForRowAtIndexPath:的整段代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"formCell"];    
 if (cell == nil) {       
 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"formCell"];       
 cell.textLabel.text = @"monalisa";   
  }    
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = @"mona";

附件上的按鈕被點擊后調用這個方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"附件按鈕被點擊");
}
將要顯示一個cell的時候會調用這個方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"將要顯示cell");
}

常用屬性

1.行高(每一行的高度都是200)

 tableView.rowHeight = 200;

2.分組的header和footer的高度

 tableView.sectionHeaderHeight = 100;
 tableView.sectionFooterHeight = 100;

3.設置分割線的樣式

 UITableViewCellSeparatorStyleNone,     (隱藏分割線)
 UITableViewCellSeparatorStyleSingleLine,
 UITableViewCellSeparatorStyleSingleLineEtched
   
 [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

4.手動刷新數據(自動刷新是在創建和滑動tableView的時候)---刷新會重新調用dataSource中創建cell的方法去重新創建cell

//刷新所有的cell
[tableView reloadData];


 //刷新指定的組
 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:5];
 [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//刷新指定的row
[self.tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]

5.將視圖添加到Header上,只能添加一個

 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
 
 view.backgroundColor = [UIColor yellowColor];
    [tableView setTableHeaderView:view];

6.設置內容偏移

[tableView setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];

7.獲取cell在當前TabView中的indexPath

 NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

8.點擊后取消cell選中效果


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {    
      // 取消選中狀態  
      [tableView deselectRowAtIndexPath:indexPath animated:NO];
 }

不常用屬性

1.設置分割線的邊距

[tableView setSeparatorInset:UIEdgeInsetsMake(50, 50, 50, 50)];

2.設置分割線的顏色

  [tableView setSeparatorColor:[UIColor redColor]];

3.獲取指定的坐標點所在的位置(第多少組第幾行)

 - ( NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;

4.獲取到指定位置(第幾組第幾行)對應的cell

 - ( UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

5.獲取當前界面上可見的所有的cell

 //@property (nonatomic, readonly) NSArray *visibleCells;

6.d

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

6.滾動到指定位置
參數1: 指定的位置
參數2: 滾動位置

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

7.將選中的cell滾動到指定位置(頂部底部或者中間)

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

8.刷新指定位置的cell

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

9.選中指定的位置

- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

TableView滾動時調用的方法

當TableView滾動時會調用該方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

當TableView停止滾動時會調用該方法

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

titles索引列

//設置背景顏色
 self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
//設置字體顏色 
self.tableView.sectionIndexColor = [UIColor blackColor];


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.indexArray;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString *key = [self.indexArray objectAtIndex:section];
    return key;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,582評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,540評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,801評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,223評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,442評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,976評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,800評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,996評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,233評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,702評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容