iOS UITableView (类似Android的ListView)(一)

学习iOS有几天了,今天看视频学到了一个UITableView控件,效果和Android的listview类似,不过封装了许多东西,比Android的listview强大多了,废话少说,开始下手。

首先,拖入一个TableView,Style改为Grouped分组

然后在ViewController.h中

接下来,开始我们ViewController.m的代码编写:

//
//  ViewController.m
//  UITableView-二维数组
//
//  Created by crw on 14-11-4.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>{
    NSArray *_allCities;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化数据
    _allCities=@[
                @[@"1"],
                @[@"21",@"22"],
                @[@"31",@"32",@"33"]
    ];
    _tableView.dataSource=self;
}

#pragma mark 一共有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allCities.count;
}
#pragma mark 第section组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //第几组的总行数
    return [_allCities[section] count];
}
#pragma mark 每一行的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil ];
    //indexPath.section第几组  indexPath.row第几行
    cell.textLabel.text=_allCities[indexPath.section][indexPath.row];
    return cell;
}



@end
看下效果:


接下来给分组加上标题:

#pragma mark 每一组的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"aa";
}
看看效果

当然,还可以加上尾部的说明

#pragma mark 每一组的尾部说明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"footer";
}

看看效果:


最后,用面向对象的思想将代码进行封装,降低耦合:

ViewControl。m

//
//  ViewController.m
//  UITableView-二维数组
//
//  Created by crw on 14-11-4.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import "ViewController.h"
#include "Province.h"

@interface ViewController ()<UITableViewDataSource>{
    NSArray *_allCities;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //封装了头部标题,尾部说明,填充数据的类
    Province *first  = [Province provinceWithHeader:@"第一组" Footer:@"第一组说明" Cities:@[@"1",@"11"]];
    Province *second = [Province provinceWithHeader:@"第二组" Footer:@"第二组说明" Cities:@[@"2",@"22"]];
    Province *third  = [Province provinceWithHeader:@"第三组" Footer:@"第三组说明" Cities:@[@"3",@"33"]];
    
    //初始化数据
    _allCities=@[first,second,third];
    _tableView.dataSource=self;
}

#pragma mark 一共有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allCities.count;
}

#pragma mark 第section组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //第几组的总行数
    return [[_allCities[section] cities] count];
}

#pragma mark 每一行的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil ];
    //indexPath.section第几组  indexPath.row第几行
    cell.textLabel.text=[_allCities[indexPath.section] cities][indexPath.row];
    
    return cell;
}

#pragma mark 每一组的头部标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_allCities[section] header];
}

#pragma mark 每一组的尾部说明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return [_allCities[section] footer];
}


@end

Province.h

//
//  Province.h
//  UITableView-二维数组
//
//  Created by crw on 14-11-5.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Province : NSObject
@property(nonatomic,copy)NSString *header;
@property(nonatomic,copy)NSString *footer;
@property(nonatomic,strong)NSArray *cities;
+(id)provinceWithHeader:(NSString *)header Footer:(NSString *)footer Cities:(NSArray *)cities;
@end

Province.m

//
//  Province.m
//  UITableView-二维数组
//
//  Created by crw on 14-11-5.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import "Province.h"

@implementation Province
+(id)provinceWithHeader:(NSString *)header Footer:(NSString *)footer Cities:(NSArray *)cities{
    Province *p=[[Province alloc] init];
    p.header=header;
    p.footer=footer;
    p.cities=cities;
    return p;
}
@end


最终效果


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
一个类似安卓ViewPager的开源库 - iOS ViewPager 高级库 支持 iPhone/ipad/ipod 相关方法 pragma mark - version 1.0 /** * 初始化 YFViewPager的方法 * * @param frame frame * @param titles 标题数组 * @param views 视图数组 和标题数组一一对应 * * @return YFViewPager */ - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles views:(NSArray *)views; /** * 设置选择的菜单按钮 * * @param index 按钮的索引值 从左到右一次是0,1,2,3... */ - (void)setSelectIndex:(NSInteger)index; /** * 点击菜单按钮时 调用的block方法 * * @param block 返回YFViewPager本身和点击的按钮的索引值,从左到右一次是0,1,2,3... */ - (void)didSelectedBlock:(SelectedBlock)block; pragma mark - version 2.0 /** * 初始化 YFViewPager的方法 也是目前使用的YFViewPager的唯一初始化api * * @param frame frame * @param titles 标题数组 * @param icons 标题右侧图标数组 * @param selectedIcons 标题右侧选中时的图标数组 * @param views 视图数组 和标题数组一一对应 * * @return YFViewPager */ - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles icons:(NSArray *)icons selectedIcons:(NSArray *)selectedIcons views:(NSArray *)views; /** * 设置菜单标题左边的icon 图标 * * @param icons 图标image * @param selectedIcons 菜单被选中时显示的图标image */ - (void)setTitleIconsArray:(NSArray *)icons selectedIconsArray:(NSArray *)selectedIcons; /** * 设置菜单右上角小红点显示的文字,数组需与菜单一一对应,数字为0时 赋值 @0或@"" * * @param tips 小红点上的文字 */ - (void)setTipsCountArray:(NSArray *)tips;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

corzfree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值