UITableView基本使用

//
//  ViewController.m
//  11-29-uitableview-lianxi2
//
//  Created by kevin on 13-11-29.
//  Copyright (c) 2013年 kevin. All rights reserved.

// 给上一个tableview加上头部标题和尾部标题,并且重构上一个lianxi1的代码
/*
 1中间使用了dictionary,怎么定义以及怎么访问,用了两个方法一个是头标题,一个是尾部标题,返回值都是nsstring,很好记。
 2中间抽取了字典的title为宏,记得以后用字典都这样写,便于同意修改title。
 */

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>
{
    NSArray *_people;
    #define header @"header"
    #define footer @"footer"
    #define cities @"cities"
} 
@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    // 二逼青年第一次写的时候忘记给tableview设置属性了,找了半天才找到错误
    tableView.dataSource = self;
    [self.view addSubview:tableView];    // 二笔青年总是忘记这一步————把视图加进去,二逼青年要好好反省。
    
    // 二逼青年试试用用dictionary来初始化带头,尾,以及People-cell内容的数组。
    _people = @[
                  @{
                      header : @"This is kevin  header description",
                      footer : @"This is kevin  footer description",
                      cities : @[@"跑车",@"豪宅",@"娇妻",@"美妾"]
                      },
                  @{
                      header : @"This is jacky  header description",
                      footer : @"This is jacky  footer description",
                      cities : @[@"拖拉机",@"破房子",@"没老婆"]
                      }
                  ];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _people.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *getProvince = _people[section];
    NSArray *getCity = getProvince[cities];   // 字典的访问也和数组类似用中括号加标示符名字访问。
    return getCity.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ // 这个方法就是设置cell的tetlabel的内容,返回的是一个cell
    static NSString *ID = @"C1";
    
    // 1.从缓存池中取出可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果缓存池中没有可循环利用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    NSDictionary *getProvince = _people[indexPath.section];
    NSArray *getCities = getProvince[cities];
    cell.textLabel.text = getCities[indexPath.row];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{   // 设置头
    NSDictionary *getHeader = _people[section];
    return getHeader[header];
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{   // 设置尾
    NSDictionary *getFooter = _people[section];
    return getFooter[footer];
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值