UITableViewy基本使用小例子

來源:互聯網
上載者:User

標籤:

//

//  MJViewController.m

//  01-UITableView01-多組數組展示

//

//  Created by apple on 13-11-28.

//  Copyright (c) 2013年 itcast. All rights reserved.

//

 

#import "MJViewController.h"

#import "Province.h"

 

 

@interface MJViewController () <UITableViewDataSource>

{

    NSArray *_allProvinces; // 所有的省份

}

@end

 

@implementation MJViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 1.添加tableView

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    tableView.dataSource = self;

    [self.view addSubview:tableView];

    

    // 2.初始化資料

    _allProvinces = @[

        [Province provinceWithHeader:@"廣東" footer:@"廣東好" cities:@[@"廣州", @"深圳"]],

        

        [Province provinceWithHeader:@"湖南" footer:@"湖南也好" cities:@[@"長沙"]],

        

        [Province provinceWithHeader:@"廣東2" footer:@"廣東好" cities:@[@"廣州", @"深圳"]],

        

        [Province provinceWithHeader:@"湖南2" footer:@"湖南也好" cities:@[@"長沙"]]

    ];

}

 

#pragma mark - 資料來源方法

#pragma mark 一共有多少組(section == 地區\組)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return _allProvinces.count;

}

 

#pragma mark 第section組一共有多少行

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

{

    // 1.取得第section組的省份

    Province *province = _allProvinces[section];

    

    // 2.取得省份裡面的城市數組

    return province.citites.count;

}

 

#pragma mark 返回每一行顯示的內容(每一行顯示怎樣的cell)

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

{

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

    

    Province *province = _allProvinces[indexPath.section];

    

    // 展示文字資料

    cell.textLabel.text = province.citites[indexPath.row];

    

    return cell;

}

 

#pragma mark 第section組顯示的頭部標題

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

//{

//    Province *province = _allProvinces[section];

//    

////    return province.header;

//    return [province header];

//}

 

#pragma mark 第section組顯示的尾部標題

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

//{

//    return [_allProvinces[section] footer];

//}

 

#pragma mark 返回表格右邊的顯示的索引條

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    NSMutableArray *titles = [NSMutableArray array];

    

    for (Province *p in _allProvinces) {

        [titles addObject:p.header];

    }

    

    return titles;

}

@end

UITableViewy基本使用小例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.