UITableView

要進(jìn)行tableView的相關(guān)設(shè)置需要遵守以下兩個(gè)協(xié)議

@interface RootViewController()<UITableViewDelegate, UITableViewDataSource>
@end

初始化

//此處y軸設(shè)為64是44的NavigationBar+20的手機(jī)導(dǎo)航欄的高度
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,64,414,736-64)];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];

一般的tableView創(chuàng)建時(shí)寫這些內(nèi)容就差不多完成了

實(shí)現(xiàn)協(xié)議方法

#pragma mark 必須實(shí)現(xiàn)的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //此方法返回的是tableView的分區(qū)數(shù)
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowInSection:(NSInteger)section{
    //此方法返回的是tableView不同分區(qū)的行數(shù)
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView CellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //此方法返回的是tableView的cell,tableView的cell使用的是重用機(jī)制,即創(chuàng)建有限個(gè)cell來(lái)應(yīng)對(duì)無(wú)限個(gè)條目,
    //當(dāng)一個(gè)cell即將在視圖中出現(xiàn)時(shí),從重用池中調(diào)用一個(gè)cell來(lái)使用,如果沒(méi)有則創(chuàng)建,對(duì)應(yīng)的當(dāng)一個(gè)cell已經(jīng)從視
    //圖中消失時(shí),將其放入重用池
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //identifier必須上下一致,這是在重用池中取用cell的標(biāo)識(shí)
    if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    
    }
    return cell;
}

此處創(chuàng)建的是系統(tǒng)提供的cell,我們實(shí)際工作中一般都會(huì)使用自定義cell。
創(chuàng)建cell除了以上方法之外還可以使用注冊(cè)模式

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//提前注冊(cè)cell的類型,自定義cell或者系統(tǒng)cell都可以這樣注冊(cè)

在返回cell的方法中

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

在前面注冊(cè)之后,后面取用的方法中需附帶設(shè)置indexPath

#pragma mark 其他方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//設(shè)置分區(qū)標(biāo)題
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//設(shè)置行高
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
//設(shè)置分區(qū)標(biāo)題視圖,和分區(qū)標(biāo)題的方法二選一即可
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
//設(shè)置分區(qū)標(biāo)題的高度
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//cell的點(diǎn)擊事件實(shí)現(xiàn)方法
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說(shuō),表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無(wú)邪8閱讀 10,963評(píng)論 3 3
  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,307評(píng)論 3 38
  • UITableViewCell控件空間構(gòu)造 cell的子控件是contentView,contentView的子控...
    CoderZXS閱讀 865評(píng)論 0 1
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,993評(píng)論 2 10
  • 序引 本系列文章將介紹iOS開(kāi)發(fā)中的UITableView控件,將會(huì)分成四篇文章完整的講述UITableView的...
    yetCode閱讀 2,421評(píng)論 3 40

友情鏈接更多精彩內(nèi)容