1.3tableView向左滑动出现多个按钮操作
//系统在iOS8以下可以参考这个网址:
http://www.jianshu.com/p/5bb91ccc07e3?utm_campaign=maleskine&utm_content=note&utm_medium=writer_share&utm_source=weibo
// ViewController.m
// 1.3tableView向左滑动出现多个按钮操作
//
// Created by 张凯泽 on 16/1/3.
// Copyright © 2016年 rytong_zkz. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *array;
@end
@implementation ViewController
//懒加载数据
-(NSMutableArray *)array
{
if (_array == nil) {
_array = [NSMutableArray array];
for (int i = 0; i<20; i++) {
[_array addObject:@(i)];
}
}
return _array;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
// return YES;
//}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * Id = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Id];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Id];
}
NSNumber * number = self.array[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@",number];
return cell;
}
#warning iOS8 -以后可用
#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
// 1. 更新数据
[self.array removeObjectAtIndex:indexPath.row];
// 2. 更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
//2. 添加一个置顶按钮
UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了置顶");
// 1. 更新数据
[self.array exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
// 2. 更新UI
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
topRowAction.backgroundColor = [UIColor blueColor];
//3. 添加一个更多按钮
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了更多");
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 将设置好的按钮放到数组中返回
return @[deleteRowAction, topRowAction, moreRowAction];
}
@end