(五)UITableView的用法一

一、表視圖的介紹

1、表視圖和其他視圖一樣,它是iOS中最重要的試圖,很多應(yīng)用程序都會使用到,表只是一個(gè)容器,要想顯示內(nèi)容,必須提供信息。

2、表試圖里面可以放很多行信息,使用表視圖可以顯示一個(gè)單元格列表,每個(gè)單元格列表都包含大量的信息,但仍然是一個(gè)整體。并且可以將表視圖劃分為多個(gè)區(qū)(section),以遍從視覺上將信息分組。表視圖控制器是一種只能顯示表視圖的標(biāo)準(zhǔn)視圖控制器,可以在表視圖占據(jù)整個(gè)視圖時(shí)使用這種控制器。

3、表視圖的兩種風(fēng)格(或者說外觀)

  1)普通風(fēng)格(或者叫無格式,它不像分組表那樣在視覺上將各個(gè)區(qū)分開,但通常帶可觸摸的索引(類似于通訊錄),有時(shí)又稱他們?yōu)樗饕恚?
    UITableViewStylePlain(段里面較多數(shù)據(jù)可以保持段頭保持不動)

  2)分組風(fēng)格(分組)

    UITableViewStyleGrouped

  3)UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實(shí)并沒有本質(zhì)區(qū)別,只是后者按分組樣式顯示前者按照普通樣式顯示而已。

二、表視圖的基本結(jié)構(gòu)

1、表視圖有表頭、表尾、中間一連串單元格試圖組成 (表頭,設(shè)置單元格視圖,表尾)

1)設(shè)置表頭 tableHeaderView (其實(shí)就是建一個(gè)label)

例如 :給tableView設(shè)置表頭

UILabel *headerLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 60)];

headerLable.textAlignment = NSTextAlignmentCenter;//表頭設(shè)置在中間

headerLable.text = @"五期聯(lián)系人";

headerLable.backgroundColor = [UIColor cyanColor];

tableView.tableHeaderView = headerLable;

2)設(shè)置單元格試圖(每個(gè)單元格都有獨(dú)特的標(biāo)示符,也成為重標(biāo)示符,(reuse)用于在編譯時(shí)引用單元格;配置表視圖時(shí),必須使用這些標(biāo)示符)

     UITableViewCell,單元格也可以分段顯示,每一段都可以通過代理設(shè)置段頭和段尾

3)設(shè)置表尾 tableFooterView

 例如:給tableView設(shè)置表尾

UILabel *footterLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 60)];

footterLable.textAlignment = NSTextAlignmentCenter;

footterLable.text = @"未來由我創(chuàng),愛拼才會贏.";

footterLable.backgroundColor = [UIColor clearColor];

tableView.tableFooterView = footterLable;
  1. tableView的常用屬性和方法(每個(gè)屬性都有舉例)如果是建立的UITabelView 表達(dá)時(shí)用 它的對象加 屬性名字,如果是基于UITableViewController,則運(yùn)用屬性直接用self.加屬性名字
      1.   設(shè)置表視圖分割線風(fēng)格

      @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;
     例如:
      self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

      self.tableView.separatorColor = [UIColor cyanColor];

     2. 設(shè)置表視圖分割線顏色,默認(rèn)標(biāo)準(zhǔn)灰色
       
     @property(nonatomic,retain) UIColor *separatorColor;
    
     例如:tableView.separatorColor = [UIColor redColor];

     3.設(shè)置表視圖的頭部視圖
 
     @property(nonatomic,retain) UIView *tableHeaderView;

     例如:給tableView設(shè)置表頭
     UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
     headerLabel.textAlignment = NSTextAlignmentCenter;
     headerLabel.text = @"寧做雞頭不做鳳尾";
     headerLabel.textColor = [UIColor whiteColor];
     headerLabel.backgroundColor = [UIColor clearColor];
     tableView.tableHeaderView = headerLabel;
     
     4. 設(shè)置表視圖的尾部視圖

     @property(nonatomic,retain) UIView *tableFooterView;
     
     例如: 給tableView設(shè)置表尾
     UILabel *footerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
     footerLabel.textAlignment = NSTextAlignmentCenter;
     footerLabel.text = @"愛咋咋地";
     footerLabel.textColor = [UIColor whiteColor];
     footerLabel.backgroundColor = [UIColor clearColor];
     tableView.tableFooterView = footerLabel;
      
     5.設(shè)置表視圖單元格的行高

     @property(nonatomic) CGFloat rowHeight;
     例如:
         tableView.rowHeight = 100;
      
     6.設(shè)置表視圖背景
  
        @property(nonatomic, readwrite, retain) UIView *backgroundView
    例如:
          tableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"apple.jpg"]];//后面是圖片名字
     
     7.刷新表視圖單元格中數(shù)據(jù)

        - (void)reloadData;

      8.顯示指示條
      
        showsVerticalScrollIndicator

     9 設(shè)置表視圖section頭部行高

        @property(nonatomic) CGFloat sectionHeaderHeight;

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

         return 40;

        }
     10. 設(shè)置表視圖section尾部部行高

        @property(nonatomic) CGFloat sectionFooterHeight

       -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
        {
           return 40;
        }

三、單元格的顯示

1、單元格的位置表示

   NSIndexPath:能表示當(dāng)前cell是tableView的第幾段第幾行

2、單元格的創(chuàng)建(必須有一個(gè)標(biāo)識符)

   UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

cell的樣式

     1)UITableViewCellStyleDefault

       左側(cè)顯示textLabel,imageView顯示在最左邊

     2)UITableViewCellStyleValue1

       左側(cè)顯示textLabel、右側(cè)顯示detailTextLabel,imageView顯示在最左邊

     3)UITableViewCellStyleValue2

        左側(cè)依次顯示textLabel(默認(rèn)藍(lán)色)和detailTextLabel,imageView可選

     4)UITableViewCellStyleSubtitle

        左上方顯示textLabel,左下方顯示detailTextLabel,imageView顯示在最左邊

    cell的輔助圖標(biāo) accessoryType (accessory |?k?ses?ri| 輔助,附加的)

     1) 不顯示任何圖標(biāo)
    
        UITableViewCellAccessoryNone, 

     2) 跳轉(zhuǎn)指示圖標(biāo)  

        UITableViewCellAccessoryDisclosureIndicator

     3) 內(nèi)容詳情圖標(biāo)和跳轉(zhuǎn)指示圖標(biāo)

        UITableViewCellAccessoryDetailDisclosureButton

     4) 勾選圖標(biāo)

         UITableViewCellAccessoryCheckmark

     5) 內(nèi)容詳情圖標(biāo)

         UITableViewCellAccessoryDetailButton 

     5) 自定義輔助圖標(biāo)

         accessoryView屬性

3、cell的常用屬性

   1)設(shè)置cell的背景試圖

      backgroundView

     
   2)設(shè)置選中的cellbei的背景圖片

      selectedBackgroundView

      cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableCell"]];


   3) 設(shè)置選中時(shí)的樣式

      selectionStyle

4.UItableView表中的每一行都由一個(gè)UItableViewCell表示可以用一個(gè)圖像,一些文本,一個(gè)可選擇的輔助圖標(biāo)來配置每個(gè)UItableViewCell對象,UItableViewCell為每個(gè)cell定義了如下的所示的屬性

      1). textLable:Cell的主文本標(biāo)簽(一個(gè)UILable的對象)

      2). detailTextLable :cell的二級文本標(biāo)簽,當(dāng)需要添加額外細(xì)節(jié)時(shí)(一個(gè)UILable對象)

      3) . imageView :一個(gè)用來裝載圖片的圖片視圖

5.UItableView 必須實(shí)現(xiàn)的3個(gè)核心方法(段數(shù),行,建立一個(gè)cell)

1).段的數(shù)量,返回表視圖劃分多少個(gè)分區(qū)(這個(gè)方法可以分段顯示或者單個(gè)列表顯示我們的數(shù)據(jù))不寫代表一行

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

2).設(shè)置某段行的數(shù)量 //返回給定分區(qū)有多少行,分區(qū)編號從0開始(不同的分段返回不同的行數(shù)可以用switch來做,如果是單個(gè)列表,就返回單個(gè)想要的函數(shù)即可)

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


3).- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//返回一個(gè)經(jīng)過正確配置的單元格對象,用于顯示在表視圖的指定位置(通過我們索引的路徑section和row來確定)

四、代理方法(UITableViewDelegate)

1、一般是處理表視圖基本樣式(單元格高度)以及捕捉選中單元格事件

2、常用代理方法(下面的返回值都是數(shù)字)

 1)配置行高

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

 2)設(shè)置section 頭部、尾部視圖的高度(段頭,段尾)

  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

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

 3)自定義section頭部、尾部視圖,注意:需要指定高度

  1.- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
例如:
//自定義段頭
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];

 NSString *title = dataArray[section][@"title"];

label.text = title;

label.backgroundColor = [UIColor redColor];

label.textAlignment = NSTextAlignmentCenter;//把自定義的段頭段位放到中間


return label;

}

//自定義段尾
  - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

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

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];

NSString *title = dataArray[section][@"trail"];

label.text = title;

label.backgroundColor = [UIColor redColor];

label.textAlignment = NSTextAlignmentCenter;


return label;

}


 4)用戶單擊單元格中輔助按鈕時(shí),調(diào)用該方法

  - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

 5)用戶單擊單元格,調(diào)用該方法(五六很像,大家要區(qū)分開)

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

 6)取消選中單元格時(shí),調(diào)用該方法

  - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

 7)設(shè)置單元格編輯樣式

  - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

  8).創(chuàng)建lable時(shí)帶的一些屬性
    //給label指定內(nèi)容
    cell.textLabel.text = self.dataArray[indexPath.row];

     //給label的內(nèi)容設(shè)置字體和大小
     cell.textLabel.font = [UIFont fontWithName:self.dataArray[indexPath.row] size:20];

     cell.textLabel.textColor = [UIColor whiteColor];

     cell.backgroundColor = [UIColor clearColor];


     9).懶加載

     當(dāng)用到某一個(gè)對象的時(shí)候,才創(chuàng)建這個(gè)對象


   - (NSArray *)dataArray
  {

   if (_dataArray == nil) {
    
    _dataArray = [NSArray array];
      }

     return _dataArray;

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

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