(新手上路)UITableVIewCell移动,标记,插入,删除,多行删除

来源:互联网 发布:淘宝天猫超市有客服吗 编辑:程序博客网 时间:2024/06/05 00:20

见完整代码

.h文件

#import <UIKit/UIKit.h>

@interface jkwmainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *TableView;



@end

.m文件
#import "jkwmainViewController.h"

@interface jkwmainViewController (){
    NSMutableArray *arr;
    UITableViewCellEditingStyle style;
    UIBarButtonItem *delete,*add,*edit,*selectdelete,*cancel;
    //*moveButton;
}

@end

@implementation jkwmainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.TableView.dataSource = self;
    self.TableView.delegate = self;
    //self.navigationItem.title = @"我是数字";
    arr = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < 50; i ++) {
        NSString *str = [NSString stringWithFormat:@"我是数字%d",i];
        [arr addObject:str];
    }
    //[self.TableView setEditing:YES animated:YES];//启动表格的编辑模式
    delete= [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(delete)];
    add = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStyleBordered target:self action:@selector(add)];
    selectdelete= [[UIBarButtonItem alloc]initWithTitle:@"选择删除" style:UIBarButtonItemStyleBordered target:self action:@selector(selectdelete)];
    NSArray *leftBarButtons = [NSArray arrayWithObjects:delete,add,selectdelete, nil];
    
    self.navigationItem.leftBarButtonItems = leftBarButtons;//设置导航栏左边按钮为添加和删除按钮
    
//    moveButton = [[UIBarButtonItem alloc]initWithTitle:@"移动" style:UIBarButtonItemStyleBordered target:self action:@selector(Move)];
   cancel= [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
    
    edit= [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
    NSArray *rightBarButtons = [NSArray arrayWithObjects:edit,cancel, nil];
    
    
    self.navigationItem.rightBarButtonItems = rightBarButtons;
    
    self.TableView.allowsMultipleSelectionDuringEditing = NO;
    
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arr count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *dentify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@ %d %d",dentify,indexPath.section,indexPath.row]];
    if (cell == nil) {
        
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    cell.textLabel.text = [arr objectAtIndex:[indexPath row]];
    cell.detailTextLabel.text = @"看看我是几";
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];

    return cell;
    
}
//标记单行 标记多少行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *onecell = [tableView cellForRowAtIndexPath:indexPath];
    if (onecell.accessoryType == UITableViewCellAccessoryNone ) {
        onecell.accessoryType  = UITableViewCellAccessoryCheckmark;
    }
    else onecell.accessoryType = UITableViewCellAccessoryNone;
    self.TableView.allowsMultipleSelection = YES;
     NSArray *selectRows = [self.TableView indexPathsForSelectedRows];//得到选中行
    selectdelete.title = [NSString stringWithFormat:@"删除(%d)",selectRows.count];
 
   //[self updateDeleteButtonTitle];
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.TableView.allowsMultipleSelection = YES;
    NSArray *selectRows = [self.TableView indexPathsForSelectedRows];//得到选中行
    selectdelete.title = [NSString stringWithFormat:@"删除(%d)",selectRows.count];
    UITableViewCell *onecell = [tableView cellForRowAtIndexPath:indexPath];

    if (onecell.accessoryType == UITableViewCellAccessoryNone ) {
        onecell.accessoryType  = UITableViewCellAccessoryCheckmark;
    }
    else onecell.accessoryType = UITableViewCellAccessoryNone;
   
    
}


// 是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//编辑模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return style;
    
}//打开编辑模式后,默认情况下每行左边会出现红的删除按钮,这个方法就是关闭这些按钮的
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
//           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return UITableViewCellEditingStyleNone;
//}
//这个方法用来告诉表格 这一行是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;//可以移动
}
//这个方法就是执行移动操作的
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSUInteger fromRow = [sourceIndexPath row];//要移动的那个cell integer
    NSUInteger toRow = [destinationIndexPath row];//要移动位置的那个clell integer
    id object = [arr objectAtIndex:fromRow];
    [arr removeObjectAtIndex:fromRow];
    [arr insertObject:object atIndex:toRow];
}


-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return @"删除";
    
}
//这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete
//还是UITableViewCellEditingStyleDelete执行删除或者插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSUInteger row = [indexPath row];
        [arr removeObjectAtIndex:row]; // 从数据源中删除
        [tableView deleteRowsAtIndexPaths:@[indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic];// 从列表中删除 [NSArray arrayWithObject:indexPath]==@[indexPath]
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert){
        [arr insertObject:@"new one" atIndex:indexPath.row ];
        [self.TableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)delete {
    style = UITableViewCellEditingStyleDelete;
    [self.TableView setEditing:!self.TableView.editing animated:YES];
    if (self.TableView.editing)
        
    {
    [self.navigationItem.leftBarButtonItem setTitle:@"完成"];
        
    }
    
    else
        
    {
    [self.navigationItem.leftBarButtonItem setTitle:@"删除"];
        
    }

}

-(void)add{
    
    style = UITableViewCellEditingStyleInsert;
    
    [self.TableView setEditing:!self.TableView.editing animated:YES];
    if (self.TableView.editing) {
        [[[self.navigationItem leftBarButtonItems]objectAtIndex:1] setTitle:@"完成"];
    }
    else{
        
        [[[self.navigationItem leftBarButtonItems]objectAtIndex:1] setTitle:@"添加"];
    }
    
    
}
-(void)cancel{
    [self.TableView setEditing:NO animated:YES];
    [self.TableView reloadData];
    selectdelete.title = @"选择删除";
    
    
}
-(void)updateDeleteButtonTitle{
    NSArray *selectRows = [self.TableView indexPathsForSelectedRows];//得到选中行
    BOOL allItemsAreSelected = selectRows.count == arr.count;//是否全选
    BOOL noItemsAreSelected = selectRows.count == 0;//选中行数是否为零
    if (allItemsAreSelected||noItemsAreSelected)
    {
        selectdelete.title = @"全部删除";// 如果是全选或者未选,则删除键为删除全部
    }
    else {
        selectdelete.title = [NSString stringWithFormat:@"删除(%d)",selectRows.count];// 否则 删除键为删除(选中行数量)
    }
    
}
-(void)edit{
    self.TableView.allowsMultipleSelectionDuringEditing =YES;// 进入可多选删除状态
    [self.TableView setEditing:YES animated:YES];
//    [self updateDeleteButtonTitle];
}
-(void)selectdelete{
    if (!self.TableView.editing) {
        return;
    }
    NSArray *selectRows = [self.TableView indexPathsForSelectedRows];//选中的行
   // BOOL deleteSpecificrows = selectRows.count > 0;// 是否删除特定的行
    //删除特定的行
    if (selectRows.count ==0||selectRows.count ==arr.count) {
        //删除全部
        [self updateDeleteButtonTitle];
        [arr removeAllObjects];
        [self.TableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

    }
    if (selectRows.count > 0||selectRows.count < arr.count) {
        // 将所选的行的索引值放在一个集合中进行批量删除
      NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
        for (NSIndexPath *selectIndex in selectRows) {
            [indicesOfItemsToDelete addIndex:selectIndex.row];
        }
        //从数据源中删除所选对应的值
        [arr removeObjectsAtIndexes:indicesOfItemsToDelete];
        //删除所选的行
        [self.TableView deleteRowsAtIndexPaths:selectRows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
     // 删除完成,退出编辑状态,并退出多选状态
    [self.TableView setEditing:NO animated:YES];
    self.TableView.allowsMultipleSelectionDuringEditing = NO;
    selectdelete.title = @"选择删除";
}
@end


0 0
原创粉丝点击