簡單描述UITableView

  • 在iOS開發(fā)中UITableView可以說是使用最廣泛的控件地沮,我們平時使用的軟件中到處都可以看到它的影子,類似于微信、QQ即硼、新浪微博等軟件都有用到UITableView。

UITableView的創(chuàng)建

  #import "ViewController.h"
  // 定義一個宏作為cell的重用池
  #define kTableViewCellReuse @"reuse"
  @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> // tableView的兩個協(xié)議
  @end
  @implementation ViewController
  - (void)viewDidLoad {
        [super viewDidLoad];
        //  創(chuàng)建tableView, 并讓其與屏幕大小一樣
        UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
        [self.view addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
        // 注冊tableViewCell
        [tableView registerClass:[UITableViewCell class]   forCellReuseIdentifier:kTableViewCellReuse];
  }
  #pragma mark - tableView的dataSource協(xié)議
  // 使其具有五個分區(qū)
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 5;
  }
  // 每個分區(qū)里面含有五個cell
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return 5;
  }
  // 在這個協(xié)議方法里面給cell賦值
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
      cell.textLabel.text = @"這是一個cell";
      return cell;
  }

tableView的創(chuàng)建需要設(shè)置兩個參數(shù), 一個是frame, 我們就不多說了, 還有一個是style, 這個style是個枚舉, 它總共有兩種style, 如下所示屡拨。

typedef NS_ENUM(NSInteger, UITableViewStyle) {
    UITableViewStylePlain,          // regular table view
    UITableViewStyleGrouped         // preferences style table view
};

下面兩張圖分別表示了UITableViewStylePlain和UITableViewStyleGrouped的顯示樣式

UITableViewStylePlain.png
UITableViewStyleGrouped.png

UITableView的頭視圖和尾視圖

     // 創(chuàng)建頭視圖View
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
    headView.backgroundColor = [UIColor redColor];
    // 創(chuàng)建尾視圖View
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
    // 將兩個View分別放在頭視圖和尾視圖上
    tableView.tableHeaderView = headView;
    tableView.tableFooterView = footView;

效果圖如圖所示, 一般我們都是在頭視圖上添加輪播圖等...

紅色部分即為tableView的頭視圖
如圖的藍(lán)綠色即為tableView的尾視圖

UITableViewCellStyle

  • UITableView中每行數(shù)據(jù)都是一個UITableViewCell只酥,在這個控件中為了顯示更多的信息,iOS已經(jīng)在其內(nèi)部設(shè)置好了多個子控件以供開發(fā)者使用呀狼。
  • 如果我們查看UITableViewCell的頭文件就可以發(fā)現(xiàn)在其內(nèi)部有一個UIView控件(contentView裂允,作為其他元素的父視圖)、兩個UILable控件(textLabel哥艇、detailTextLabel)绝编、一個UIImage控件(imageView),分別用于容器、顯示內(nèi)容十饥、詳情和圖片窟勃。
  • 這些控件不一定要使用, 因?yàn)槲覀兛梢宰远xtableViewCell來實(shí)現(xiàn)一些系統(tǒng)做不了的事情, 這些控件的布局屬性有UItableViewCellStyle進(jìn)行設(shè)置.比如以下代碼.
 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuse"];
  • 上面的代碼是UITableViewCell的初始化方法, 可以通過其設(shè)置UITableViewCellStyle, 它是一個枚舉值:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) { 
UITableViewCellStyleDefault, // 左側(cè)顯示textLabel(不顯示detailTextLabel),imageView可選(顯示在最左邊) 
UITableViewCellStyleValue1, // 左側(cè)顯示textLabel逗堵、右側(cè)顯示detailTextLabel(默認(rèn)藍(lán)色)秉氧,imageView可選(顯示在最左邊) 
UITableViewCellStyleValue2, // 左側(cè)依次顯示textLabel(默認(rèn)藍(lán)色)和detailTextLabel,imageView可選(顯示在最左邊)
 UITableViewCellStyleSubtitle // 左上方顯示textLabel蜒秤,左下方顯示detailTextLabel(默認(rèn)灰色),imageView可選(顯示在最左邊) 
};

下面介紹幾個常用的tableView的協(xié)議方法

#pragma mark - 設(shè)置每行高度(每行高度可以不一樣, 通過判斷實(shí)現(xiàn))
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 45;
 } 
#pragma mark - 設(shè)置尾部標(biāo)題內(nèi)容高度
 -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
    return 40; 
}
#pragma mark - 返回每組頭標(biāo)題名稱
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *headTitle = [NSString stringWithFormat:@"第%ld個分區(qū)的頭視圖", section];
    return headTitle;
}
#pragma mark - 返回每組尾部說明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NSString *footTitle = [NSString stringWithFormat:@"第%ld個分區(qū)的尾視圖", section];
    return footTitle;
}
#pragma mark - 返回每組標(biāo)題索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *indexsArray = [[NSMutableArray alloc]init];
    indexsArray = @[@"A", @"B", @"C", @"D", @"E"].mutableCopy;
    return indexsArray;
}
#pragma mark - 設(shè)置分組頭視圖標(biāo)題內(nèi)容高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    // 通過section的不同可以設(shè)置每個頭視圖標(biāo)題的高度
    if(section==0) {
        return 50;
    }
    return 40;
}
#pragma mark - 設(shè)置每行(Row)的高度(每行高度可以不一樣)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
#pragma mark - 設(shè)置尾部說明內(nèi)容高度(每個尾視圖高度可以不同)
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}
#pragma mark - tableView的點(diǎn)擊方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"點(diǎn)擊了第%ld分區(qū)的第%ld個cell", indexPath.section, indexPath.row);
}

設(shè)置每個分區(qū)頭視圖和尾視圖的View

#pragma mark - 返回每組尾視圖的View
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    footView.backgroundColor = [UIColor greenColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    label.text = @"這個是每個分區(qū)的尾視圖的label";
// 判斷當(dāng)?shù)谝粋€分區(qū)的時候, 顯示的是footView, 其它的是label
    if (section == 0) {
        return footView;
    } else {
    return label;
    }
}
#pragma mark - 返回每組頭視圖的View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    headView.backgroundColor = [UIColor blueColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    label.text = @"這個是每個分區(qū)的頭視圖的label";
// 判斷當(dāng)?shù)谝粋€分區(qū)的時候, 顯示的是headView, 其它的是label
    if (section == 0) {
        return headView;
    } else {
        return label;
    }
}

下面我們看一下效果

tableView的頭視圖和尾視圖自定義view.gif

就寫到這里了, 希望有錯誤的大家指出, 互相學(xué)習(xí)互相進(jìn)步, 以后我還會對應(yīng)的寫一些關(guān)于UITableView的內(nèi)容, 歡迎大家一起討論學(xué)習(xí)汁咏。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市作媚,隨后出現(xiàn)的幾起案子攘滩,更是在濱河造成了極大的恐慌,老刑警劉巖纸泡,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件漂问,死亡現(xiàn)場離奇詭異,居然都是意外死亡女揭,警方通過查閱死者的電腦和手機(jī)级解,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來田绑,“玉大人,你說我怎么就攤上這事抡爹⊙谇” “怎么了?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵冬竟,是天一觀的道長欧穴。 經(jīng)常有香客問我,道長泵殴,這世上最難降的妖魔是什么涮帘? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮笑诅,結(jié)果婚禮上调缨,老公的妹妹穿的比我還像新娘。我一直安慰自己吆你,他們只是感情好弦叶,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著妇多,像睡著了一般伤哺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天立莉,我揣著相機(jī)與錄音绢彤,去河邊找鬼。 笑死蜓耻,一個胖子當(dāng)著我的面吹牛茫舶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播媒熊,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼奇适,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了芦鳍?” 一聲冷哼從身側(cè)響起嚷往,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎柠衅,沒想到半個月后皮仁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡菲宴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年贷祈,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喝峦。...
    茶點(diǎn)故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡势誊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谣蠢,到底是詐尸還是另有隱情粟耻,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布眉踱,位于F島的核電站挤忙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏谈喳。R本人自食惡果不足惜册烈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望婿禽。 院中可真熱鬧赏僧,春花似錦、人聲如沸扭倾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吆录。三九已至窑滞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背哀卫。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工巨坊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人此改。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓趾撵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親共啃。 傳聞我的和親對象是個殘疾皇子占调,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評論 2 361

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

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子移剪,類似...
    liudhkk閱讀 9,067評論 3 38
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多究珊,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,493評論 1 14
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改變導(dǎo)航欄背景顏...
    SadMine閱讀 1,595評論 1 4
  • 1.DOM0 事件和DOM2級在事件監(jiān)聽使用方式上有什么區(qū)別纵苛? 在DOM0級事件處理程序剿涮,事件名以'on'開頭,如...
    leocz閱讀 214評論 0 0
  • 老子云“四體不勤五谷不分攻人,孰為夫子取试?”,看來在老子眼中怀吻,會種地是夫子的必備條件瞬浓。 我來自農(nóng)村,早年在寒暑...
    李磊1026閱讀 769評論 0 1