UI_UITableView编辑
来源:互联网 发布:qq超市数据清零 编辑:程序博客网 时间:2024/05/16 10:38
#import "RootViewController.h"@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>@property(nonatomic, retain)UITableView *tableView;@property(nonatomic, retain)NSMutableArray *arr;@end@implementation RootViewController- (void)dealloc{ [_arr release]; [_tableView release]; [super dealloc];}- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self createData]; } return self;}- (void)createData { self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.delegate = self; self.tableView.dataSource = self; 1.给导航控制器右侧添加编辑按钮. self.navigationItem.rightBarButtonItem = self.editButtonItem; // 开启编辑模式,如果开启编辑模式后,那么一直处于编辑状态.// [self.tableView setEditing:YES animated:YES]; }
2.重写系统的编辑按钮触发方法,为了使右侧的编辑按钮和编辑模式相关联.- (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:animated];}
3.设置哪些行可以进行编辑操作.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // 这个方法默认是在所有行都可以进行编辑. if (indexPath.row % 2 == 0) { return YES; } else { return NO; }}
4.修改cell的编辑模式,默认是删除,还可以进行添加等操作.- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert;}
5.实现删除功能,还自带了一个左划显示删除功能的效果.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除一定要删除对应数据源的数据. [self.arr removeObjectAtIndex:indexPath.row]; // 1.增加一个删除的动画效果 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 2.开启刷新后,就是去删除的动画效果// [self.tableView reloadData]; }}
6.在默认情况下,修改delete按钮的标题.- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"哈哈";}
7.在delete按钮位置添加多个按钮,如果执行此方法,那么修改delete按钮的标题的方法就不好使了. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"添加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // 要实现的功能,都写在block里 NSLog(@"测试一下"); }]; firstAction.backgroundColor = [UIColor greenColor]; UITableViewRowAction *secondAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"通话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"hhhhh"); }]; return @[firstAction, secondAction];}
8.移动- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 点击编辑可以移动,但是并没有改变数据源中数据的顺序.// for (NSString *str in self.arr) {// NSLog(@"%@", str);// } // 修改数据源. // 1.取出数据. // 注意内存问题. NSString *str = [self.arr[sourceIndexPath.row] retain]; // 2.把数据在原来位置上移除掉 [self.arr removeObjectAtIndex:sourceIndexPath.row]; // 3.把对象移到最终的位置. [self.arr insertObject:str atIndex:destinationIndexPath.row]; [str release]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease]; } cell.textLabel.text = self.arr[indexPath.row]; return cell;}
9.单行修改数据,并刷新- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.arr replaceObjectAtIndex:indexPath.row withObject:@"刘超"];// [tableView reloadData]; // 单行刷新 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
0 0
- UI_UITableView编辑
- UI_UITableView编辑
- UI_UITableView 编辑与移动
- UI_UITableView
- UI_UITableView
- UI_UITableView异步加载图片
- UI_UITableView _新知识_02
- UI_UITableView自定义Cell
- UI_UITableView省市区字典数组
- UI_UITableView的HeaderView
- 编辑
- UI_UITableView界面传值(将前面的属性传值和协议传值结合起来)
- bianji编辑编辑
- 编辑控件
- 歌词编辑
- datagrid编辑
- 点线编辑
- 商务编辑
- 英语语法 限定词
- hdu4501(三维背包)
- RouterOS(ROS)安装
- bzoj1041
- 解档与归档
- UI_UITableView编辑
- TeX排版记录 多行长公式 括号 对齐 问题
- java的hashtable与hashMap 的讲解
- Java实现双向链表
- 【BZOJ3609】 【Heoi2014】人人尽说江南好【贪心】
- 鞍点
- UML类图几种关系的总结
- Hbase笔记五:Hbase和Hive
- split命令使用详解