今天看一下折合表和表的编辑,折合表就像QQ中的好友列表那样,能开能合,而表的编辑之前我们提到过,涉及到cell的删除与移动。
下面来看一下代码实现
@interface ViewController (){
NSArray *_titleArray;
NSMutableArray *_bigArray;
// BOOL _isOpen;
UITableView *_tableView;
// 创建bool 数组
BOOL _isOpen[4];
}
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 548) style:UITableViewStyleGrouped];
_tableView.dataSource = self;
_tableView.delegate = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
_tableView.sectionFooterHeight = 0;
// tableView.sectionHeaderHeight = 50;
[self.view addSubview:_tableView];
// 创建数据源
_titleArray = @[@"屌丝",@"美女",@"美食",@"汽车"];
NSArray *dsArray = @[@"",@"",@""];
NSArray *girlArray = @[@"范冰冰",@"李冰冰",@"凤姐",@"瘦瘦"];
NSArray *foodArray = @[@"烩面",@"大盘",@"胡辣汤",@"麻辣烫",@"辣条"];
NSArray *carArray = @[@"qq",@"大黄蜂",@"幻影",@"大野"];
_bigArray = [[NSMutableArray alloc] initWithObjects:dsArray,girlArray,foodArray,carArray, nil];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// 当程序运行时_isOpen为no,全部区域行为0
if (_isOpen[section] == NO){
return 0;
}
// 设置每个区不同的行数,从大数组中取小数组的对象
NSArray *smllArray = [_bigArray objectAtIndex:section];
return smllArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSArray *smallArray = [_bigArray objectAtIndex:indexPath.section];
cell.textLabel.text = [smallArray objectAtIndex:indexPath.row];
return cell;
}
//自定义区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor purpleColor];
UIButton *openBtn = [UIButton buttonWithType:UIButtonTypeCustom];
openBtn.tag = section;
openBtn.frame = CGRectMake(10, 10, 40, 30);
[openBtn addTarget:self action:@selector(openClick:) forControlEvents:UIControlEventTouchUpInside];
[openBtn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateNormal];
[view addSubview:openBtn];
if (_isOpen[section] == YES){
//transform 翻转
// 让按钮旋转90度
openBtn.transform = CGAffineTransformMakeRotation(M_PI_2);
}
UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(140, 10, 50, 40)];
titleLab.textColor = [UIColor whiteColor];
titleLab.text = [_titleArray objectAtIndex:section];
[view addSubview:titleLab];
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
addBtn.frame = CGRectMake(200, 10, 40, 40);
[addBtn addTarget:self action:@selector(addContact:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:addBtn];
// if (_isOpen[section]==NO)
// {
// addBtn.hidden = YES;
// }
// else{
// addBtn.hidden = NO;
// }
// 三目运算符 等价于以上注释的if判断
// 如果_isOpen == yes 取冒号前边值
// 如果_isOpen == no 取冒号后边值
addBtn.hidden = _isOpen[section]? NO : YES;
// 这样写也可以
// addBtn.hidden = !_isOpen[section];
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(250, 10, 40, 40);
[deleteBtn setTitle:@"编辑" forState:UIControlStateNormal];
// [deleteBtn ];
[deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:deleteBtn];
return view;
}
//添加联系人
- (void)addContact:(UIButton *)btn{
}
//打开或者关闭
- (void)openClick:(UIButton *)btn{
// 来改变当前点击按钮所在的某个区所对应的bool值的状态
_isOpen[btn.tag] = !_isOpen[btn.tag];
// 刷新表 这样写可以,但是会刷新所有的数据
// [_tableView reloadData];
// 把所想要刷新的区加入到索引集合中
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:btn.tag];
// 可变的索引集合
// NSMutableIndexSet *indexSet1 = [[NSMutableIndexSet alloc] init];
// [indexSet1 addIndex:1];
// [indexSet1 addIndex:2];
[_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}
//删除联系人
- (void)deleteBtnClick:(UIButton *)btn{
if (_isOpen[btn.tag]) {
_tableView.editing = YES;
}
}
//cell的删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld=======%ld",indexPath.section,indexPath.row);
}
//cell的移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"原来的位置 : %ld=======%ld",sourceIndexPath.section,sourceIndexPath.row);
NSLog(@"变换后的位置的位置 : %ld=======%ld",destinationIndexPath.section,destinationIndexPath.row);
}
那效果如何呢?来看一下折合的表现
点击编辑的时候可以编辑表,包括删除和移动,删除的时候我们可以拿到删除cell的坐标
按住cell右侧的三道杠就可以移动cell了,我们可以拿到移动前的位置坐标和移动后的位置坐标