#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (retain) UITableView* myTableView;
//定义一个可变数组,来存放所有分区的内容
@property (retain) NSMutableArray* dataSource;
//声明一个属性记录当前表格的编辑状态
@property (assign) BOOL isEdit;
@end
@implementation ViewController
@synthesize myTableView; //别名
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.automaticallyAdjustsScrollViewInsets = NO;
_isEdit = NO;
_dataSource = [NSMutableArray new];
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStyleGrouped];
[self.view addSubview:myTableView];
for (NSInteger i = 0; i < 2; i++) {
NSMutableArray* arr = [NSMutableArray new];
for (NSInteger j = 0; j < 10; j++) {
if (i == 0) {
NSString* str1 = [NSString stringWithFormat:@"男嘉宾%ld号", (long)j];
[arr addObject:str1];
}else{
NSString* str2 = [NSString stringWithFormat:@"女嘉宾%ld号", (long)j];
[arr addObject:str2];
}
}
[_dataSource addObject:arr];
}
myTableView.delegate = self;
myTableView.dataSource = self;
//显示表格编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//实现控制表格编辑状态开启与关闭的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
_isEdit =! _isEdit;
//开启表格
[myTableView setEditing:_isEdit animated:YES];
}
//返回表格有多少组,在分组风格的表格中这个方法是必须实现的
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"分区");
return _dataSource.count;
}
//返回每组有多少行,无论是什么风格的表格都必须实现的,section就是区或者说是组的索引值,我们可以根据这个索引值来找当前的小数组,然后在返回小数组的长度
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"行");
return [_dataSource[section] count];
}
//UITableViewCell 单元格,这个方法是告诉表格每行显示什么样的单元格,单元格上显示哪些内容,内容包括标题,图片,挂件等
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"单元格");
//创建一个静态的字符串,当做单元格的标记
static NSString* cellID = @"cell";
//向表格视图的单元格池中获取有该标记的单元格,获取到的单元格是暂时没有被使用的
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//创建单元格,第一个参数是单元格的类型,第二个参数是单元格的标记,如果不使用复用机制第二个参数可以给nul
//UITableViewCellStyleDefault 默认
//如果不存在就创建
if (cell == nil) {
//创建单元格
//UITableViewCellStyleDefault 风格
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
//设置标题,因为标题在数据源的小数组里面,所有必须先取到小数组,才能取到真正的标题内容,小数组的获取可以通过区的索引值来获取,标题可以通过行的索引值来获取,也就是indexPath, indexPath里面就包含了当前单元格的区索引值和行的索引值,indexPath.section是曲的索引值,indexPath.row是行的索引值
//设置单元格的标题
cell.textLabel.text = _dataSource[indexPath.section] [indexPath.row];
//设置单元格选中风格
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
//设置每个区的头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"========男嘉宾========";
}
return @"========女嘉宾========";
}
//返回每个区的的头部视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
//返回当前行是否允许编辑,可以通过indexPath参数实现控制那些区 那些行,可以编辑,不可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//返回当前编辑行的风格
/*
UITableViewCellEditingStyleNone
UITableViewCellEditingStyleDelete 删除
UITableViewCellEditingStyleInsert 添加
*/
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleInsert;
}
}
//根据编辑风格做不同的操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray* arr = _dataSource[indexPath.section];
[arr removeObjectAtIndex:indexPath.row];
//没有动画删除
//[tableView reloadData];
//有动画删除
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}else{
NSMutableArray* arr = _dataSource[indexPath.section];
[arr insertObject:@"陈令成" atIndex:indexPath.row];
//动画添加
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
//返回当前行是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSMutableArray* arr1 = _dataSource[sourceIndexPath.section];
NSString* sourceStr = arr1[sourceIndexPath.row];
[arr1 removeObjectAtIndex:sourceIndexPath.row];
NSMutableArray* arr2 = _dataSource[destinationIndexPath.section];
[arr2 insertObject:sourceStr atIndex:destinationIndexPath.row];
[tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end