1.移动
//返回当前哪个区中的那些行是什么样的编辑样式,默认的是Delete 删除 还有Insert 插入 ,None 什么都没有
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone; //None类型 表示什么都没有 那么我们在让表处于编辑状态的时候,就会显示移动状态
}
//返回当前哪些去中的哪些行是可以移动的,如果返回YES 则代表所有的行都可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0) return NO; //第一行不能移动,但是可以将其他行移动到第一行,成为第一行;成为第一行后就不能移动
return YES;
}
//移动某个单元格到哪个地方 第一个参数表示移动开始的位置 第二个参数表示 要移动到的位置 也就数据的位置交换
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath: (NSIndexPath *)sourceIndexPath toIndexPath: (NSIndexPath *)destinationIndexPath{
//从数组中找到要移动的是哪一行
NSString * moveStr = [heroArray objectAtIndex:sourceIndexPath.row];
// 先把上一次要移动的数据从数组中删除,然后在把数据添加进来
[heroArray removeObjectAtIndexPath:sourceIndexPath.row];
//把找到移动的行移动到目的位置
[heroArray insertObject:moveStr atIndex:destinationIndexPath.row];
//最后, 刷新表格
[tableView reloadData];
}
//当前表处于编辑状态时,是否有缩进效果 默认是YES
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO; //只有在UITableViewCellEditingStyleNone类型下才可以有效
}
//可以控制移动的行最终放在哪里, 自动控制当前移动的单元格移动到那个地方
- (NSIndexPath *)tableView: (UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath * )proposeDestinationIndexPath{
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:3 inSection:0]; //第0区 第3行移动到其他地方会自动弹回来
return indexPath;
}
2.编辑
//设置当前表中哪个区哪个行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//实现下面的这个方法就可以使用滑动 删除的操作
- (void)tableView: (UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath{
//根据当前的区号 取出对应的数组
NSMutableArray * array = [dataArray objectAtIndex:indexPath.section];
if (editingStyle == UITableViewCellEditingStyleDelete){
//删除操作
[array removeObjectAtIndex:indexPath.row];
}
if(editingStyle == UITableViewCellEditingStyleInsert){
//插入操作
[array addObject:@"林妹妹"];
}
//最后,刷新列表
[tableView reloadData];
}
//修改delete按钮的标题
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
3.打开关闭对应的区
//返回每一个区的区头视图的方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section{
//这里创建区头视图用的也是和单元格一样的方法,重用思想
static NSString * headerViewID = @"headerViewID";
UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier: headerID];
if (!headerView){
headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier: headerViewID];
}
return headerView;
}
在区头里定义一个button 让button的点击事件与section联系起来//用一个自定义的属性clickSection来记录
在button的监听事件里记得刷新表
在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//使用打开标记 来判断当前区是不是展开状态,如果是展开状态则返回当前区应该返回的行数,如果是关闭状态则返回0;
}
打开标记: ①C语言方法
声明全局BOOL类型 BOOL _isOpen[3];//C语言的数组 默认其中的数据为[0,0,0]
在点击区头的button监听事件里 _isOpen[button.clickSection] = !_isOpen[button.clickSection];//标记哪些区是展开的
在返回单元格行的方法里
if (_isOpen[section] == YES) {
return [[dataArray objectAtIndex:section] count];
}
return 0;
② oc语言里
声明一个 全局数组
NSMutableArray * isOpenArray;//默认当前所有区都是关闭状态
isOpenArray = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO], nil];
然后在button监听事件里先还原成BOOL取反再替换,再返回行数里取出对应元素转BOOL 判断再确定行 ,这种比较麻烦 ,oc里兼容c ,可以使用c比较简单
4.刷新表
[self.tableView reloadData]
该方法在刷新表的时候,都会把表的代理方法走一遍:
返回区数方法仅执行一次 —>
执行区头的高度———>
执行区头(有多少个区就会执行多少次)————>
每个区中返回多少行(有多少个区执行多少次)————>
返回每一个单元格 (执行次数为区数*行数).