Table左滑删除并添加多个按钮实例
来源:互联网 发布:win2008流量监控软件 编辑:程序博客网 时间:2024/06/05 06:15
跟标题一样,就是写了table的左划删除功能,并且可以添加多按钮,当然下面也多写了很多代理方法,算是福利吧。
感谢飞飞大神的博客供这篇文章的学习:侧滑删除。
下面直接上代码注释都写好了:
#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self makeTable]; // Do any additional setup after loading the view, typically from a nib.}#pragma mark 做table- (void)makeTable{ UITableView *demoTab = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; demoTab.delegate = self; demoTab.dataSource = self; [self.view addSubview:demoTab];}#pragma mark table代理//有几段-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}//有几行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10;}//cell-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *iden = @"iden"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden]; } cell.textLabel.text = @"我就是个占位的"; return cell; }//对编辑的状态下提交的事件响应-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"d");}//让表格可以修改,滑动可以修改- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//让行可以移动-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//table向左滑动时的代理- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"我向左滑动啦~"); return UITableViewCellEditingStyleDelete;}//好吧滑动以后的几个按钮这里来写- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ //建立一个按钮叫删除 UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { //点击删除时使用的方法 NSLog(@"删除"); //允许开启编辑单元格 [tableView setEditing:YES animated:YES]; }]; //给删除按钮设置一个颜色 layTopRowAction1.backgroundColor = [UIColor redColor]; //建立第二个按钮更多 UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { //点击更多出现一个Sheet选择 NSLog(@"更多"); UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"创建待办", @"标为未读", @"标为红旗", @"移动", @"这是垃圾邮件",nil]; actionSheet.actionSheetStyle = UIBarStyleBlackOpaque; [actionSheet showInView:self.view]; //允许开启编辑单元格 [tableView setEditing:YES animated:YES]; }]; //设置第二个按钮的颜色 layTopRowAction2.backgroundColor = [UIColor grayColor]; //最后保存好两个按钮统一放在数组中传递回去 NSArray *arr = @[layTopRowAction1,layTopRowAction2]; return arr; }#pragma mark sheet的代理-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0:NSLog(@"创建待办"); break; case 1:NSLog(@"标为未读"); break; case 2:NSLog(@"标为红旗"); break; case 3:NSLog(@"移动"); break; case 4:NSLog(@"这是垃圾邮件"); break; case 5:NSLog(@"取消"); break; } }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
感谢观看,学以致用更感谢!
1 0
- Table左滑删除并添加多个按钮实例
- tableviewcell添加多个按钮删除事件
- UITableView左滑出现多个按钮
- 按钮触发table添加一行删除一行
- iOS8 table view添加左滑出现按钮组的方法
- [js]多个按钮点击添加div,再点击删除
- android 左滑显示删除按钮SwipeMenuListView
- 左滑删除按钮的新思路
- 左滑tableview出现删除按钮
- 动态添加个table并绑定gridview
- 在table中通过按钮添加一行或者删除一行
- UITableView侧滑添加多个按钮
- iOS左滑插入多个按钮,按钮自定义颜色、图片、文字字体等
- js操作 添加删除table行,并进行刷新
- jquery应用实例一动态添加删除生成表格table
- 基于jQuery的左滑出现删除按钮
- 高仿微信实现左滑显示删除按钮功能
- android 类似qq左滑露出删除按钮
- Elasticsearch API操作01
- PropertyUtils的使用
- ab性能测试工具参数说明
- @using (Html.BeginForm())参数示例
- instantclient(Oracle即时客户端)的安装与配置
- Table左滑删除并添加多个按钮实例
- final finally finalize的使用区别
- fatal error RC1015: cannot open include file 'l.chs\\afxres.rc'
- WIN10本地搭建APACHE+PHP运行环境
- Eclipse导入Tomcat源码
- 分享下工作经验
- UIview需要知道的一些事情:setNeedsDisplay、setNeedsLayout
- 颜色模型
- Activity , Window和View的关系