UI10_TableView的编辑

来源:互联网 发布:淘宝上的福袋能买吗 编辑:程序博客网 时间:2024/06/03 19:26
#import "MainViewController.h"@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>@property(nonatomic, retain)UITableView *tableView;@property(nonatomic, retain)NSMutableArray *arr;@end@implementation MainViewController- (void)dealloc{    [_tableView release];    [_arr release];    [super dealloc];}- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];    }    return self;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent = NO;    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];    // 右边编辑完成按钮.系统自带    self.navigationItem.rightBarButtonItem = self.editButtonItem;    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667- 64) style:UITableViewStylePlain];    [self.view addSubview:self.tableView];    [_tableView release];    self.tableView.dataSource = self;    self.tableView.delegate = self;//    //直接打开tableView的编辑按钮模式//    [self.tableView setEditing:YES animated:YES];}#pragma mark 重写系统的编辑按钮点击- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    [super setEditing:editing animated:animated];    [self.tableView setEditing:editing animated:YES];}#pragma mark 设置哪些行可以进行编辑,默认YES;-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}#pragma mark 有三种样式,一种是插入,一种是删除.默认是删除.还有一个是无.- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row % 2 == 0) {        return UITableViewCellEditingStyleInsert;    }else{        return UITableViewCellEditingStyleDelete;    }}#pragma mark 删除数据 这个方法默认提供了一个左划出来按钮.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        // 1.删除数据源        [self.arr removeObjectAtIndex:indexPath.row];        // table 删除        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];        [tableView reloadData];        // 添加数据    }else if (editingStyle == UITableViewCellEditingStyleInsert){        [self.arr addObject:self.arr[indexPath.row]];        [tableView reloadData];    }}#pragma mark 修改delete按钮标题- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{    return @"删除";}#pragma mark 这个方法是iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮.实现置顶与删除
0 0
原创粉丝点击