#import "ViewController.h"
#define SCREEN_BOUNDS ([UIScreen mainScreen].bounds)
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface ViewController ()
{
NSMutableArray *list;
UITableView *TableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
TableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/2) style:UITableViewStyleGrouped];
TableView.backgroundColor = [UIColor whiteColor];
TableView.delegate = self;
TableView.dataSource = self;
[self.view addSubview:TableView];
list = [NSMutableArray arrayWithObjects:@"人生快事",@"学无止境",@"俯瞰星空",@"游遍全国",@"不枉此生", nil];
UIBarButtonItem *barButton = [[UIBarButtonItemalloc]initWithTitle:@"Jay-Z" style:UIBarButtonItemStylePlain target:selfaction:@selector(start:)];
self.navigationItem.rightBarButtonItem= barButton;
}
- (void)start:(UIBarButtonItem*)sender{
TableView.editing = !TableView.editing;
}
#pragma mark---必须实现的两个代理方法---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"ID";
//在tableView上查找是否有可以复用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = list[indexPath.row];
return cell;
}
#pragma mark--设置编辑状态、移动的代理方法--
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
//移动
- (BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//提交编辑的时候 调用
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[list removeObjectAtIndex:indexPath.row];
//更新视图 只更新一行
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
[list insertObject:@"添加的cell内容" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
//专门用于移动的时候调用
- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
//1、保存资源文件
NSString *source = list[sourceIndexPath.row];
//2、移除
[list removeObjectAtIndex:sourceIndexPath.row];
//3、插入数据
[list insertObject:sourceatIndex:destinationIndexPath.row];
//更新视图的方法
[tableView moveRowAtIndexPath:sourceIndexPathtoIndexPath:destinationIndexPath];
}
#pragma mark---设置表头、表尾的代理方法---
//设置表头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
//设置表尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 50;
}
//设置表头的视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIViewalloc]init];
view.backgroundColor = [UIColor redColor];
return view;
}
//设置表尾的视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIViewalloc]init];
//可以添加按钮到view上
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(SCREEN_WIDTH/3, 0, SCREEN_WIDTH/3, 50);
button.backgroundColor = [UIColor orangeColor];
[view addSubview:button];
return view;
}
//设置表头的标题
- (nullable NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
return @"表头";
}
//设置表尾的标题
- (nullable NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
return @"表尾";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end