UI11_TableView编辑
2015-10-05 08:43
399 查看
AppDelegate.h
AppDelegate.m
RootViewController.h
RootViewController.m
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [_window release]; RootViewController *rootVC = [[RootViewController alloc] init]; UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC]; self.window.rootViewController = naVC; [rootVC release]; [naVC release]; return YES; }
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.m
#import "RootViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @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, WIDTH, HEIGHT) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.dataSource = self; self.tableView.delegate = self; // 给导航控制器右侧添加一个编辑按钮 self.navigationItem.rightBarButtonItem = self.editButtonItem; // 开启编辑模式 // [self.tableView setEditing:YES animated:YES]; } // 重写系统的编辑按钮触发方法 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:animated]; } // 设置哪些行可以进行编辑 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // 这个方法默认就是所有行都可以进行编辑 // if (indexPath.row % 2 == 1) { // return YES; // } else { // return NO; // } return YES; } // 修改Cell的编辑模式, 默认是删除, 还可以进行添加等操作 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } // 实现删除功能 // 还自带一个左划显示删除功能的效果 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除一定要删除对应数据源的数据 [self.arr removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [self.tableView reloadData]; } } - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"fdas"; } - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"添加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { // 要实现的功能, 都写在block里 NSLog(@"测试一下"); }]; UITableViewRowAction *secondAction = [UITableViewRowAction rowActionWithStyle:0 title:@"游骑兵" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"2"); }]; firstAction.backgroundColor = [UIColor blackColor]; return @[firstAction, secondAction]; } // 移动 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // NSLog(@"111"); // 修改数据源 // 1. 取出数据 // 注意内存问题 NSString *str = [self.arr[sourceIndexPath.row] retain]; // 2. 把数据在原来位置上移除掉 [self.arr removeObjectAtIndex:sourceIndexPath.row]; // 3. 把对象移到最终的位置 [self.arr insertObject:str atIndex:destinationIndexPath.row]; [str release]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]; } cell.textLabel.text = self.arr[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.arr replaceObjectAtIndex:indexPath.row withObject:@"刘超"]; // [tableView reloadData]; // 单行刷新 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr.count; }
相关文章推荐
- 【白痴弟弟和你加强应用层】阅读 Develop API Guides 思考(一个)
- UI11_数据处理
- UI_UITableViewCell自适应高度(图片,字体)
- UI10_省市区分区跳转版
- SpriteBuilder中时间线播放音效的弊端
- SpriteBuilder中时间线播放音效的弊端
- SpriteBuilder中时间线播放音效的弊端
- UITableView中的一些坑
- SpriteBuilder中音频波长超过Timeline结尾的情况
- SpriteBuilder中音频波长超过Timeline结尾的情况
- SpriteBuilder中音频波长超过Timeline结尾的情况
- 2015/10/4 iOS 笔记 细节 简单-代理过程 UITableView
- HDOJ 题目5496 Beauty of Sequence(数学)
- iOS UISwitch用法和示例
- hdu 5496 Beauty of Sequence bc#58 组合 离散化
- Android后台进程与UI进程的交互
- Fluent NHibernate关系映射
- POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)
- Android UI ActionBar功能-ActionBarSherlock 的使用
- Android UI ActionBar功能-ActionBarSherlock 的使用