//继承于UITableViewController的视图控制器 上面有一个自带的视图UITableView 表格视图的显示位置及大小都是默认的 此处无需再定义表格视图
//设置表格的分区个数
//如果表格视图有一个分区 那么该方法的返回值就为1 或者直接将该方法注释 系统默认表格就是一个分区
//【注意】该方法是可选择实现的方法
//如果方法返回值为0 就会造成程序崩溃
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// 下面的方法是必须要实现的方法
//<1>返回每个分区中的行数
//如果表格只有一个分区 那么就直接返回数据源的元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//<2>设置的是每一个单元格(行)显示的内容(文字信息、图片信息、控件信息、挂件信息.....)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//表格视图上的单元格采用的是可重用机制
//也就是表格视图创建出来以后 编译器会为表格创建一个屏幕多1个单元格 多出的单元格会在内存中等候 直到屏幕上有一个单元格划出屏幕 那么多出的那个单元格就会去屏幕的最上面或者最下面补位 划出的那个单元格就会在内存中继续等候 等候下一次复用
//划出屏幕多少个单元格就有多少个单元格可复用
//<1>可重用的单元格都会使用一个静态局部字符串作为标识
static NSString * str = @"identifier";
//该静态局部变量的名称任意 内容也任意
//<2>先去队列中查找是否存在用上面定义的字符串标识的可重用单元格
//单元格的类名 UITableViewCell
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
/<3>判断这样的单元格是否存在
if(cell == nil)//--------重点(表示单元格的重用)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
//创建出可重用的单元格 使用str标识一下
}
//<4>为单元格添加内容
//indexPath 是当前屏幕上显示的表格区域
//我们可以通过indexPath获取显示在屏幕上的分区的区号、分区中的行号
//区号和行号的下标值都是从0开始
//所以经常使用行号获取数组中下标对应的内容
//行号的获取indexPath.row
cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];
=======================自定义表格=========================================
//<1>创建表格对象
/*
UITableViewStylePlain,
UITableViewStyleGrouped
*/
//以上两个值只是设置表格的显示样式,与有无分组无关
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 480 - 64) style:UITableViewStylePlain]
//<2>设置代理(如果不设置代理 协议中的方法调用不到)
table.delegate = self;
table.dataSource = self;
//<3>表格视图是滚动视图的子类
self.automaticallyAdjustsScrollViewInsets = NO;
//<4>初始化数据源
dataSource = [[NSMutableArray alloc]init];
//设置分区的头标题
//可选择实现的方法
//【注意】plain样式的表格视图 如果不设置分区的头标题 那么分区样式显示不出来
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//设置脚标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//设置行高 ---- 单元格的高度默认为44像素
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//【注意】group样式的表格 不设置分区的头标题 分区样式也显示的很明显 组的模式
==============表格视图的美化====================
在创建单元格方法内
//为单元格添加图片 图片的显示位置在单元格的左侧
cell.imageView.image = [imageArr objectAtIndex:indexPath.row];
//为单元格添加挂件
/*
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks
UITableViewCellAccessoryCheckmark, // checkmark. doesn't track
UITableViewCellAccessoryDetailButton
*/
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// 为单元格添加控件信息 (使用我们学习过的任何一种控件作作为当前单元格的挂件)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 100, 30); // 按钮的显示位置是无效值
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = indexPath.row + 1; // 将自定义的挂件与cell进行关联
[btn setTitle:@"跳转" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
cell.accessoryView = btn;
// 设置单元格点击后的背景颜色
/*
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault
*/
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// 单元格的点击事件
// 可选择的
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
===============表格的编辑======================
//在导航条上添加编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//<1>开启表格视图的编辑模式(编辑按钮无论是Edit还是Done都会调用该方法)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//1、调用父类中的编辑方法
[super setEditing:editing animated:YES];
//2、改变表格的编辑状态
isEditing = !isEditing;
//3、设置表格的编辑样式
[table setEditing:isEditing animated:YES];
}
//<2>设置单元格的编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
return UITableViewCellEditingStyleDelete;
}
else
{
return UITableViewCellEditingStyleInsert;
}
}
//<3>单元格的编辑方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//1、删除 先删除数组中的元素 在表格视图中的单元格
//2、添加 先向数组中添加元素 再在表格视图中插入单元格
if(editingStyle == UITableViewCellEditingStyleDelete)
{
NSMutableArray * deleteArr = [dataSource objectAtIndex:indexPath.section];
[deleteArr removeObjectAtIndex:indexPath.row];
//1、删除的单元格所在的区域的数组
//2、自带的动画效果
[table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
NSMutableArray * insertArr = [dataSource objectAtIndex:indexPath.section];
[insertArr insertObject:@"我是新来的" atIndex:indexPath.row];
[table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
//<4>开启所有单元格的移动状态
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//<5>实现移动方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//<1>sourceIndexPath将要移动的单元格所在的区域
//<2>destinationIndexPath 单元格移动到哪个区域
//实现方法:将移动的元素从它所在的数组中删除 然后再插入到移动到的数组中
NSMutableArray * deleteArr = [dataSource objectAtIndex:sourceIndexPath.section];
NSString * deleteStr = [deleteArr objectAtIndex:sourceIndexPath.row];
[deleteArr removeObject:deleteStr];
NSMutableArray * insertArr = [dataSource objectAtIndex:destinationIndexPath.section];
[insertArr insertObject:deleteStr atIndex:destinationIndexPath.row];
}
UITableView取消选中颜色、常用操作
http://www.cnblogs.com/zcw-ios/articles/2574372.html