UITableview控件簡(jiǎn)單介紹??

  • 版權(quán)聲明:本文為博主原創(chuàng)文章绞铃,未經(jīng)博主允許不得轉(zhuǎn)載很泊。

一努咐、基本介紹

在眾多移動(dòng)應(yīng)?用中,能看到各式各樣的表格數(shù)據(jù) 苦蒿。

在iOS中,要實(shí)現(xiàn)表格數(shù)據(jù)展示,最常用的做法就是使用UITableView,UITableView繼承自UIScrollView,因此支持垂直滾動(dòng),?且性能極佳 渗稍。

UITableview有分組和不分組兩種樣式佩迟,可以在storyboard或者是用代碼或者是Xib設(shè)置。

二竿屹、數(shù)據(jù)展示

UITableView需要?一個(gè)數(shù)據(jù)源(dataSource)來(lái)顯示數(shù)據(jù)
UITableView會(huì)向數(shù)據(jù)源查詢一共有多少行數(shù)據(jù)以及每?行顯示什么數(shù)據(jù)等

沒(méi)有設(shè)置數(shù)據(jù)源的UITableView只是個(gè)空殼

凡是遵守UITableViewDataSource協(xié)議的OC對(duì)象,都可以是UITableView的數(shù)據(jù)源

展示數(shù)據(jù)的過(guò)程:
(1)調(diào)用數(shù)據(jù)源的下面?法得知?一共有多少組數(shù)據(jù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

(2)調(diào)用數(shù)據(jù)源的下面?法得知每一組有多少行數(shù)據(jù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

(3)調(diào)?數(shù)據(jù)源的下??法得知每??顯示什么內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

三报强、代碼示例

  • (1)能基本展示的“垃圾”代碼
#import "JBViewController.h"

@interface JBViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation JBViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 設(shè)置tableView的數(shù)據(jù)源
    self.tableView.dataSource = self;
    
}

#pragma mark - UITableViewDataSource
/**
 *  1.告訴tableview一共有多少組
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"numberOfSectionsInTableView");
    return 2;
}
/**
 *  2.第section組有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection %d", section);
    if (0 == section) {
        // 第0組有多少行
        return 2;
    }else
    {
        // 第1組有多少行
        return 3;
    }
}
/**
 *  3.告知系統(tǒng)每一行顯示什么內(nèi)容
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
//    indexPath.section; // 第幾組
//    indexPath.row; // 第幾行
    // 1.創(chuàng)建cell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    // 2.設(shè)置數(shù)據(jù)
    // cell.textLabel.text = @"汽車";
    // 判斷是第幾組的第幾行
    if (0 == indexPath.section) { // 第0組
        if (0 == indexPath.row) // 第0組第0行
        {
            cell.textLabel.text = @"奧迪";
        }else if (1 == indexPath.row) // 第0組第1行
        {
            cell.textLabel.text = @"寶馬";
        }
        
    }else if (1 == indexPath.section) // 第1組
    {
        if (0 == indexPath.row) { // 第0組第0行
            cell.textLabel.text = @"本田";
        }else if (1 == indexPath.row) // 第0組第1行
        {
            cell.textLabel.text = @"豐田";
        }else if (2 == indexPath.row) // 第0組第2行
        {
            cell.textLabel.text = @"馬自達(dá)";
        }
    }
    
    // 3.返回要顯示的控件
    return cell;
    
}
/**
 *  第section組頭部顯示什么標(biāo)題
 *
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // return @"標(biāo)題";
    if (0 == section) {
        return @"德系品牌";
    }else
    {
        return @"日韓品牌";
    }
}
/**
 *  第section組底部顯示什么標(biāo)題
 *
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (0 == section) {
        return @"高端大氣上檔次";
    }else
    {
        return @"還不錯(cuò)";
    }
}
@end```



![](http://upload-images.jianshu.io/upload_images/838345-6915ab01843c2c95.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


- (2)讓代碼的數(shù)據(jù)獨(dú)立

新建一個(gè)模型:

```objc

#import <Foundation/Foundation.h>

@interface JBCarGroup : NSObject
/**
 *  標(biāo)題
 */
@property (nonatomic, copy) NSString *title;
/**
 *  描述
 */
@property (nonatomic, copy) NSString *desc;
/**
 *  當(dāng)前組所有行的數(shù)據(jù)
 */
@property (nonatomic, strong) NSArray *cars;

@end```



```objc
#import "JBViewController.h"
#import "JBCarGroup.h"

@interface JBViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
 *  保存所有組的數(shù)據(jù)(其中每一元素都是一個(gè)模型對(duì)象)
 */
@property (nonatomic, strong) NSArray *carGroups;
@end


@implementation JBViewController


#pragma mark - 懶加載
- (NSArray *)carGroups
{
    if (_carGroups == nil) {
        // 1.創(chuàng)建模型
        JBCarGroup *cg1 = [[JBCarGroup alloc] init];
        cg1.title = @"德系品牌";
        cg1.desc = @"高端大氣上檔次";
        cg1.cars = @[@"奧迪", @"寶馬"];
        
        JBCarGroup *cg2 = [[JBCarGroup alloc] init];
        cg2.title = @"日韓品牌";
        cg2.desc = @"還不錯(cuò)";
        cg2.cars = @[@"本田", @"豐田", @"小田田"];
        
        JBCarGroup *cg3 = [[JBCarGroup alloc] init];
        cg3.title = @"歐美品牌";
        cg3.desc = @"價(jià)格昂貴";
        cg3.cars = @[@"勞斯萊斯", @"布加迪", @"小米"];
        // 2.將模型添加到數(shù)組中
        _carGroups = @[cg1, cg2, cg3];
    }
    // 3.返回?cái)?shù)組
    return _carGroups;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 設(shè)置tableView的數(shù)據(jù)源
    self.tableView.dataSource = self;
    
}

#pragma mark - UITableViewDataSource
/**
 *  1.告訴tableview一共有多少組
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"numberOfSectionsInTableView");
    return self.carGroups.count;
}
/**
 *  2.第section組有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection %d", section);
    // 1.取出對(duì)應(yīng)的組模型
    JBCarGroup *g = self.carGroups[section];
    // 2.返回對(duì)應(yīng)組的行數(shù)
    return g.cars.count;
}
/**
 *  3.告知系統(tǒng)每一行顯示什么內(nèi)容
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
//    indexPath.section; // 第幾組
//    indexPath.row; // 第幾行
    // 1.創(chuàng)建cell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    // 2.設(shè)置數(shù)據(jù)
    // cell.textLabel.text = @"嗨嘍";
    // 2.1取出對(duì)應(yīng)組的模型
    JBCarGroup *g = self.carGroups[indexPath.section];
    // 2.2取出對(duì)應(yīng)行的數(shù)據(jù)
    NSString *name = g.cars[indexPath.row];
    // 2.3設(shè)置cell要顯示的數(shù)據(jù)
    cell.textLabel.text = name;
    // 3.返回要顯示的控件
    return cell;
    
}
/**
 *  第section組頭部顯示什么標(biāo)題
 *
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // return @"標(biāo)題";
    // 1.取出對(duì)應(yīng)的組模型
   JBJCarGroup *g = self.carGroups[section];
    return g.title;
}
/**
 *  第section組底部顯示什么標(biāo)題
 *
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    // return @"標(biāo)題";
    // 1.取出對(duì)應(yīng)的組模型
    JBCarGroup *g = self.carGroups[section];
    return g.desc;
}
@end```

- 實(shí)現(xiàn)效果:


![](http://upload-images.jianshu.io/upload_images/838345-5f70f16697924eac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


>提示:請(qǐng)自行體會(huì)把數(shù)據(jù)獨(dú)立出來(lái)單獨(dú)處理,作為數(shù)據(jù)模型的好處拱燃。另外秉溉,把什么抽成一個(gè)模型,一定要弄清楚。

###四召嘶、補(bǔ)充點(diǎn)

> contentView下默認(rèn)有3個(gè)?視圖:

>第2個(gè)是UILabel(通過(guò)UITableViewCell的textLabel和detailTextLabel屬性訪問(wèn))

>第3個(gè)是UIImageView(通過(guò)UITableViewCell的imageView屬性訪問(wèn))

> UITableViewCell還有?個(gè)UITableViewCellStyle屬性,?于決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置 

![](http://upload-images.jianshu.io/upload_images/838345-83ca0d48d3e7078f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末父晶,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子弄跌,更是在濱河造成了極大的恐慌甲喝,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,807評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铛只,死亡現(xiàn)場(chǎng)離奇詭異埠胖,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)淳玩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門押袍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人凯肋,你說(shuō)我怎么就攤上這事谊惭。” “怎么了侮东?”我有些...
    開封第一講書人閱讀 169,589評(píng)論 0 363
  • 文/不壞的土叔 我叫張陵圈盔,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我悄雅,道長(zhǎng)驱敲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評(píng)論 1 300
  • 正文 為了忘掉前任宽闲,我火速辦了婚禮众眨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘容诬。我一直安慰自己娩梨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評(píng)論 6 398
  • 文/花漫 我一把揭開白布览徒。 她就那樣靜靜地躺著狈定,像睡著了一般。 火紅的嫁衣襯著肌膚如雪习蓬。 梳的紋絲不亂的頭發(fā)上纽什,一...
    開封第一講書人閱讀 52,785評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音躲叼,去河邊找鬼芦缰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛枫慷,可吹牛的內(nèi)容都是我干的让蕾。 我是一名探鬼主播浪规,決...
    沈念sama閱讀 41,220評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼涕俗!你這毒婦竟也來(lái)了罗丰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤再姑,失蹤者是張志新(化名)和其女友劉穎萌抵,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體元镀,經(jīng)...
    沈念sama閱讀 46,698評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡绍填,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評(píng)論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了栖疑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片讨永。...
    茶點(diǎn)故事閱讀 40,912評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖遇革,靈堂內(nèi)的尸體忽然破棺而出卿闹,到底是詐尸還是另有隱情,我是刑警寧澤萝快,帶...
    沈念sama閱讀 36,572評(píng)論 5 351
  • 正文 年R本政府宣布锻霎,位于F島的核電站,受9級(jí)特大地震影響揪漩,放射性物質(zhì)發(fā)生泄漏旋恼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評(píng)論 3 336
  • 文/蒙蒙 一奄容、第九天 我趴在偏房一處隱蔽的房頂上張望冰更。 院中可真熱鬧,春花似錦昂勒、人聲如沸蜀细。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)审葬。三九已至,卻和暖如春奕谭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背痴荐。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工血柳, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人生兆。 一個(gè)月前我還...
    沈念sama閱讀 49,359評(píng)論 3 379
  • 正文 我出身青樓难捌,卻偏偏與公主長(zhǎng)得像膝宁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子根吁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評(píng)論 2 361

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