zoukankan      html  css  js  c++  java
  • UITableViewy基本使用小例子

    //

    //  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

  • 相关阅读:
    OpenGL ES应用开发实践指南:iOS卷
    WCF(1)----服务创建
    算法设计--电路布线问题(分支限界法求解)
    Oracle 删除用户和表空间
    从最简单的源代码开始,切勿眼高手低---(第一波)
    pinyin4j的使用
    ios学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
    ArcGIS多面体(multipatch)解析——引
    [珠玑之椟]位向量/位图的定义和应用
    搭建自己的XenServer+CloudStack云平台,提供IaaS服务(一)环境搭建
  • 原文地址:https://www.cnblogs.com/xgj0721/p/4340296.html
Copyright © 2011-2022 走看看