表格视图
UITableView
tableview的大部分操作都是在代理方法中进行的!!!
//UITableViewCell 视图,tableView中每一行都是一个UITableViewCell对象
//indexPath (section,row,用于描述此行数据位于第几分区,第几行)
//UITableViewCell 对象,被赋好值后,返回给tableView
/*UITableViewCell的重用机制
*1、不是有多少行数据,就创建多少个cell,每个UITableView都有一个可重用的队列,滑出屏幕的cell,会被先放到可重用队列中
*2、UITableView的重用机制,最大限度的节省了程序的内存开销,提高了程序的运行效率,重用机制对于程序的开发具有非凡的借鉴意义
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的可重用标识符
static NSString *cellId = @"cell";
//根据cell的可重用标识符,到tableView的可重用队列中,获取cell对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
//拿不到,则alloc init
//初始化cell,并设置cell的样式,给cell赋值可重用标识符
//UITableViewCellStyleSubtitle 主标题,副标题样式
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];
//设置cell选中的风格样式
cell.selectionStyle = UITableViewCellSelectionStyleGray;
//设置cell右边的附属按钮样式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//blue
}
//给cell的赋值操作,写在cell == nil之外
//根据分区下标从_data中获取array
NSArray *array = [_data objectAtIndex:indexPath.section];
//根据行的下标,取到array中字符串
NSString *str = [array objectAtIndex:indexPath.row];
//设置cell的主标题
cell.textLabel.text = str;
//设置cell的副标题
cell.detailTextLabel.text = @"副标题";
return cell;
}
//每个分区里的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//分区数量,默认1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//分区的头标题和脚标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
//行高(默认44),分区头标题高度,脚标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
//将一个view设成头标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
//选中表格视图某一行后,触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//行被选中后,自动变回反选状态的方法
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
//必须有蓝色的按钮出现,才能点击执行这个方法,点出附属按钮
NSLog(@“点击了第%d分区%d行",indexPath.section,indexPath.row);
}
#pragma mark - editing 下面都是编辑相关
- (void)customEditMethod
{//自定义按钮实现编辑方法时,需要设置一个bool型成员变量
_isEditing = !_isEditing;
[_tableView setEditing:_isEditing animated:YES];
}
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//作用系统自带的编辑按钮时必须按照下面的方式重写编辑函数,固定写法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[myTableView setEditing:editing animated:animated];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{//编辑的方法,如果其它的编辑都不设,只设了这一个,那么就会有cell右滑出现删除的按钮,也是比较常用的
if (editingStyle == UITableViewCellEditingStyleDelete){
//删除
[[dataArr objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row]; //删除数据源记录
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//删除cell
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
//插入
[[dataArr objectAtIndex:indexPath.section] insertObject:@"a new cell" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{//删除按钮的字
return @"删";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回编辑模式,默认是删除
if (indexPath.section) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//移动,不实现这个代理方法,就不会出现移动的按钮
NSString *text=[[[dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] copy];
[[dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
[[dataArr objectAtIndex:destinationIndexPath.section] insertObject:text atIndex:destinationIndexPath.row];
[text release];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{//默认所有的都是可以编辑的(增,删)
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{//默认所有的都是可以移动的
return YES;
}
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
//作为数据源
NSMutableArray *_dataArr;
//表格视图
UITableView *_myTableView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
}
- (void)createData
{
_dataArr = [[NSMutableArray alloc] init];
for (int i = 0; i<20; i++) {
[_dataArr addObject:[NSString stringWithFormat:@"第%d牛宝宝",i]];
}
}
- (void)createUI
{
//创建表格视图
_myTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
//2个代理!!!!!!
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//设置tableView的行数
return [_dataArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//cell即将出现时调用
//indexPath.row代表的是cell的行数
NSLog(@"第%d个cell即将出现",indexPath.row);
//一个静态的字符串用来作为cell的标识符kl;
static NSString *identifier = @"cellID";
//通过标识符,在tableView的复用队列里查询cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
//如果在复用队列里没有查询结果,那么就创建一个新的cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
NSLog(@"=====%d",indexPath.row);
}
//if条件走完以后,肯定会有一个cell,需要修改cell里的内容
//从数据源里查询对应位置的元素,让cell展示
cell.textLabel.text = [_dataArr objectAtIndex:indexPath.row];
return cell;
}
#import "ViewController.h"
#import "Person.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
UITableView *_myTableView;
NSMutableArray *_dataArr;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
}
- (void)createData
{
_dataArr = [[NSMutableArray alloc] init];
NSMutableArray *boyArr = [NSMutableArray array];
for (int i = 0; i<20; i++) {
Person *xx = [[Person alloc] init];
xx.name = [NSString stringWithFormat:@"男%d",i];
xx.age = [NSString stringWithFormat:@"%d",arc4random_uniform(9)+20];
[boyArr addObject:xx];
}
[_dataArr addObject:boyArr];
NSMutableArray *girlArr = [NSMutableArray array];
for (int i = 0; i<21; i++) {
Person *gg = [[Person alloc] init];
gg.name = [NSString stringWithFormat:@"女%d",i];
gg.age = [NSString stringWithFormat:@"%d",arc4random_uniform(10)+18];
[girlArr addObject:gg];
}
[_dataArr addObject:girlArr];
}
- (void)createUI
{
self.view.backgroundColor = [UIColor redColor];
_myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, 410) style:UITableViewStylePlain];
_myTableView.dataSource = self;
_myTableView.delegate = self;
//分割线的颜色
_myTableView.separatorColor = [UIColor redColor];
[self.view addSubview:_myTableView];
}
#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{//设置分段个数
return [_dataArr count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//根据分段,设置行数
return [[_dataArr objectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{//设置分段的头标题
if (!section) {
return @"男";
}
return @"女";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{//设置分段的脚标题
if (!section) {
return @"男脚";
}
return @"女脚";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//设置行高(cell的高度,默认是44)
return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{//分段头标题的高度
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{//分段脚标题的高度
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{//自定义分段的头标题
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
label.text = @"自定义头标题";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor greenColor];
return label;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//每一行里展示的内容
//indexPath代表cell的位置,其中section是段数,row是行数
NSLog(@"%d====%d",indexPath.section,indexPath.row);
static NSString *qqq = @"qq";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:qqq];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:qqq];
//cell被点击时,是否变灰
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDetailButton;
}
Person *xiao = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = xiao.name;
cell.detailTextLabel.text = xiao.age;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"第%d段,第%d行",indexPath.section,indexPath.row);
//点击变灰的cell,恢复成默认状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//通过点击的cell的位置找到对应的数据模型。
Person *xiao = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
NSLog(@"age = %@",xiao.age);
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{//cell自带的附属btn的点击事件
NSLog(@"附属的:第%d段,第%d行",indexPath.section,indexPath.row);
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *age;
@end
#import "ViewController.h"
#import "Person.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
UITableView *_myTableView;
NSMutableArray *_dataArr;
BOOL _isEdit;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
}
- (void)createData
{
_dataArr = [[NSMutableArray alloc] init];
NSMutableArray *boyArr = [NSMutableArray array];
for (int i = 0; i<20; i++) {
Person *xx = [[Person alloc] init];
xx.name = [NSString stringWithFormat:@"轩地在要要男%d",i];
xx.age = [NSString stringWithFormat:@"在地上上是主和%d",arc4random_uniform(9)+20];
[boyArr addObject:xx];
}
[_dataArr addObject:boyArr];
NSMutableArray *girlArr = [NSMutableArray array];
for (int i = 0; i<21; i++) {
Person *gg = [[Person alloc] init];
gg.name = [NSString stringWithFormat:@"女%d",i];
gg.age = [NSString stringWithFormat:@"%d",arc4random_uniform(10)+18];
[girlArr addObject:gg];
}
[_dataArr addObject:girlArr];
}
- (void)createUI
{
self.view.backgroundColor = [UIColor redColor];
_myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, 410)];
_myTableView.dataSource = self;
_myTableView.delegate = self;
[self.view addSubview:_myTableView];
//创建一个工具条
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 40)];
[self.view addSubview:toolbar];
//创建一个专用按钮
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editClick)];
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClick)];
//设置工具条上展示的专用按钮(通过数组)
toolbar.items = [NSArray arrayWithObjects:editItem, refreshItem, nil];
}
- (void)refreshClick
{
//重载tv的代理,(刷新数据)
[_myTableView reloadData];
}
- (void)editClick
{
_isEdit = !_isEdit;
//每次点击时,修改tv的编辑状态
[_myTableView setEditing:_isEdit animated:YES];
}
#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{//设置分段个数
return [_dataArr count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//根据分段,设置行数
return [[_dataArr objectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (!section) {
return @"男人的世界";
}
return @"女人世界";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//每一行里展示的内容
//indexPath代表cell的位置,其中section是段数,row是行数
NSLog(@"%d====%d",indexPath.section,indexPath.row);
static NSString *qqq = @"qq";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:qqq];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:qqq];
}
Person *xiao = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = xiao.name;
cell.detailTextLabel.text = xiao.age;
return cell;
}
#pragma mark - tableViewEdit
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{//设置删除按钮的文字
return @"d";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{//编辑的事件
//判断编辑风格
if (editingStyle == UITableViewCellEditingStyleDelete) {
//先从数据源中删除对应的数据模型
[[_dataArr objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
//删除对应的cell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
Person *xiao = [[Person alloc] init];
xiao.name = @"新来的";
xiao.age = @"18";
//在对应的位置中插入数据模型
[[_dataArr objectAtIndex:indexPath.section] insertObject:xiao atIndex:indexPath.row];
//在对应的位置插入cell
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//设置tv的编辑风格
if (indexPath.section) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//移动事件
//用一个指针指向要移动的数据源
Person *xiao = [[_dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
//将数据源中需要移动的对象删除
[[_dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
//在新位置插入
[[_dataArr objectAtIndex:destinationIndexPath.section] insertObject:xiao atIndex:destinationIndexPath.row];
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *age;
@end