2015-12-24-TavleView编辑

//
// AppDelegate.h
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder

@property (retain, nonatomic) UIWindow *window;

@end


//
// AppDelegate.m
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

  • (void)dealloc
    {
    [_window release];
    [super dealloc];
    }

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ViewController *rootVC = [[ViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rootVC];
    self.window.rootViewController = navi;

    [rootVC release];
    [navi release];

[_window release];
return YES;

}

  • (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end



//
// ViewController.h
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


//
// ViewController.m
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "ViewController.h"
#import "CellTableViewCell.h"
#import "Model.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    // 编辑状态
    // [_tableView setEditing:YES animated:YES];
    // 编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // 自定义 按钮
    UIButton *edit = [UIButton buttonWithType:UIButtonTypeSystem];
    [edit setTitle:@"开启" forState:UIControlStateNormal];
    [edit setTitle:@"关闭" forState:UIControlStateSelected];
    edit.frame = CGRectMake(0, 0, 50, 50);
    [edit addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:edit];
// 数据
[self dataHandler];

}
// 按钮响应方法

  • (void)editAction:(UIButton *)btn
    {
    // BOOL类型状态翻转 选中与取消
    btn.selected = !btn.selected;
    // 通过button状态 控制tableView
    [_tableView setEditing:btn.selected animated:YES];
    }
    // 编辑按钮 可以触发编辑方法

  • (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
    // 状态翻转 没有则无法翻转
    [super setEditing:editing animated:animated];
    // 使用编辑按钮 控制tableView的编辑状态
    [_tableView setEditing:editing animated:animated];
    }

  • (void)dataHandler
    {
    self.dataArr = [@[@"大娃", @"二娃", @"三娃", @"四娃", @"五娃", @"六娃", @"七娃", @"八娃", @"九娃", @"十娃"]mutableCopy];
    // self.dataArr = [NSMutableArray array];
    // for (NSString *str in arr) {

    // }
    }
    /// 是否允许编辑(BOOL)

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 0 是下标
    // if (indexPath.row == 0) {
    // return NO;
    // } else{
    // return YES;
    // }
    return YES;
    }
    // 设置cell编辑样式

  • (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 默认样式为 删除
    // 插入
    // return UITableViewCellEditingStyleInsert;
    // 复选 多选
    // return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
    // 删除
    return UITableViewCellEditingStyleDelete;

}
// 删除确认信息

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return @"你瞅什";
    }
    // 通过编辑状态 判断编辑操作
  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 通过参数editingStyle判断 执行不同操作
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    // 删除操作 分两部分: 数据/视图
    // 先操作 数据 在同步更新视图
    // 1.从数组中删除数据
    [_dataArr removeObjectAtIndex:indexPath.row];
    // 2.同步视图 保存到数组
    // 参数1: 视图操作cell位置
    // 参数2: 操作的动画选项
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    }
    /// 移动
    // 参数2. 源位置信息
    // 参数3. 目的地(目标位置信息)
  • (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
    // 1. 设置临时变量 保存要移动的信息
    #warning 注意: 不能确定的要移动数据的引用计数 为了防止被提前释放 在临时变量接收时 需要持有一次
    NSString *temp = _dataArr[sourceIndexPath.row];
    // 2.从原位置移除
    [_dataArr removeObjectAtIndex:sourceIndexPath.row];
    // 3. 添加到目标位置
    [_dataArr insertObject:temp atIndex:destinationIndexPath.row];
    // 4.内存管理
    [temp release];
    // 只要移动方法被实现 就能拥有移动动画 可以不写视图匹配操作

}
// 限制跨区移动
// 参数2. 原位置信息
// 参数3. 预期目标位置信息
// 参数4.

  • (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
    {
    // 判断预期位置 是否和原位置在一个分区
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
    return proposedDestinationIndexPath;
    }else{
    // 如果不在一个分区 回到原位置
    return sourceIndexPath;
    }
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return _dataArr.count;
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
    }
    cell.textLabel.text = _dataArr[indexPath.row];
    return cell;

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end



//
// CellTableViewCell.h
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CellTableViewCell : UITableViewCell

@end


//
// CellTableViewCell.m
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "CellTableViewCell.h"

@implementation CellTableViewCell

  • (void)awakeFromNib {
    // Initialization code
    }

  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    }

@end



//
// Model.h
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Model : NSObject

@end


//
// Model.m
// UI12_TavleView编辑
//
// Created by YIem on 15/12/22.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "Model.h"

@implementation Model

@end

标签: none

添加新评论

Title - Artist
0:00