AppDelegate.h
#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;
}