tableView不光只是用来显示数据的,tableView的编辑可以帮我们实现好多功能,这些功能的方法系统帮我们写好了,我们只要调用系统的方法就能轻松实现了.
下面是具体的代码:
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊", nil];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//UIscrollView的属性:添加额外的滚动附近区域的内容
self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
UIImageView *imageV= [[UIImageView alloc]initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, 200)];
imageV.image = [UIImage imageNamed:@"08.jpg"];
//self.tableView.tableHeaderView = imageV;
imageV.tag = 100;
[self.tableView addSubview:imageV];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"%@",[scrollView class]);
//以后当页面里同时存在tableview和scrollview时,要事先判断当前是哪个控件触发的协议方法,避免因为没有判断,造成错误的操作.
CGFloat yOffest = self.tableView.contentOffset.y;
UIImageView *imageV= (UIImageView *)[self.view viewWithTag:100];
if (yOffest < 0) {
imageV.frame = CGRectMake(0, yOffest, self.view.frame.size.width, -yOffest);
}
}
//重写系统的编辑按钮的点击方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
#pragma mark -设置哪些行可以进行编辑,通过返回yes和no来判断.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//每行都可以编辑
return YES;
}
#pragma mark -设置编辑的样式.
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
#pragma mark - 左划出现删除
//状态只有是delete的时候可以左划出现删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//根据不同的判断结果实现不同的效果
if (editingStyle == UITableViewCellEditingStyleDelete) {
//要实现删除,第一件事是删除数组里对应的数据
[self.arr removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
//将要消失的行加动画,消失画面变得柔和
//[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
- (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"你猜" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//在block里,写对应rowaction的点击事件
NSLog(@"22");
}];
UITableViewRowAction *secondAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"我不猜" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
}];
firstAction.backgroundColor = [UIColor purpleColor];
secondAction.backgroundColor = [UIColor blueColor];
return @[firstAction,secondAction];
}
//tableview的移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//1.先获取要移动的数据
NSString *str = self.arr[sourceIndexPath.row];
//2.把数组里对应的字符串从数组中移除掉
[self.arr removeObjectAtIndex:sourceIndexPath.row];
[self.arr insertObject:str atIndex:destinationIndexPath.row];
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = self.arr[indexPath.row];
return cell;
}
下面是运行的结果: