学习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
最终效果