iOS程序-UITableView分组展示数据

来源:互联网 发布:json字符串格式 编辑:程序博客网 时间:2024/05/09 23:12

UITableView分组展示数据,并设置每一组的头部标题和尾部标题。

numberOfSectionsInTableView:设置组数

numberOfRowsInSection:设置每一组的行数

cellForRowAtIndexPath:设置每一行显示的内容和样

titleForHeaderInSection:设置显示的头部标题

titleForFooterInSection:设置显示的尾部标题



方法/步骤

  1. 1

    MJViewController.h

    #import <UIKit/UIKit.h>

    @interface MJViewController : UIViewController

    @end

  2. 2

    MJViewController.m

    #import "MJViewController.h"

    // 省份字典中用到的key

    #define kHeader @"header" // 头部标题对应的key

    #define kFooter @"footer" // 尾部标题对应的key

    #define kCities @"cities" // 城市数组对应的key

    @interface MJViewController () <UITableViewDataSource>

    {

    //    NSArray *_allCities; // 所有的城市

        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 = @[

                          

                          @{

                              kHeader : @"广东",

                              kFooter : @"广东好",

                              kCities : @[@"广州", @"深圳", @"梅州"]

                          },

                          

                          @{

                              kHeader : @"湖南",

                              kFooter : @"湖南也好",

                              kCities : @[@"长沙", @"益阳"]

                           },

                          

                          @{

                              kHeader : @"湖北",

                              kFooter : @"湖北更好",

                              kCities : @[@"武汉", @"黄冈"]

                              }

            ];

        

    //    _allCities = @[

    //                   ,

    //                   ,

    //                   @[@"武汉", @"黄冈"],

    //                   @[@"桂林", @"玉林"],

    //                   @[@"杭州", @"温州"],

    //                   @[@"合肥", @"安庆"]

    //    ];

    }

    #pragma mark - 数据源方法

    #pragma mark 一共有多少组(section == 区域\组)

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return _allProvinces.count;

    }

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

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

    {

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

        NSDictionary *province = _allProvinces[section];

        

        // 2.取得省份里面的城市数组

        NSArray *cities = province[kCities];

        

        return cities.count;

        

    //    // 1.取得第section组的所有城市

    //    NSArray *sectionCities = _allCities[section];

    //    

    //    // 2.第section组城市的个数

    //    return sectionCities.count;

    }

    #pragma mark 返回每一行显示的内容(每一行显示怎样的cell)

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

    {

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

        

    //    NSString *text = _allCities[indexPath.section][indexPath.row];

    //    NSArray *sectionCities = _allCities[indexPath.section];

        

        // 1.取出第section组第row行的文字数据

        // 取出第section组的省份 中 城市数组里面 第 row行的 数据

        

        NSDictionary *province = _allProvinces[indexPath.section];

        NSArray *cities = province[kCities];

        

        NSString *text = cities[indexPath.row];

        

        // 2.展示文字数据

        cell.textLabel.text = text;

        return cell;

    }

    #pragma mark 第section组显示的头部标题

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

    {

    //    if (section == 0) return @"广东";

    //    if (section == 1) return @"湖南";

    //    if (section == 2) return @"湖北";

    //    if (section == 3) return @"广西";

    //    if (section == 4) return @"浙江";

    //    if (section == 5) return @"安徽";

        

        NSDictionary *province = _allProvinces[section];

        

        return province[kHeader];

    }

    #pragma mark 第section组显示的尾部标题

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

    {

    //    if (section == 0) return @"广东好";

    //    if (section == 1) return @"湖南也好";

    //    if (section == 2) return @"湖北更好";

    //    if (section == 3) return @"广西一般般";

    //    if (section == 4) return @"浙江应该可以吧";

    //    if (section == 5) return @"安徽确实有点坑爹";

        

        return _allProvinces[section][kFooter];

    }

    @end

0 0