editActionsForRowAtIndexPath(iOS8) tableview編輯(刪除、插入、移動)

ios8 出來的左滑小菜單 能夠自定義想要的按鈕 (要求ios8以上)ios

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        NSLog(@"-----------=點擊刪除");
        
    }];
    
    
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"編輯" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        NSLog(@"-----------=點擊編輯");
        
    }];
    
    UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置頂" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        
        NSLog(@"-----------=點擊置頂");
        
    }];
    
    UITableViewRowAction *signAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"標記未讀" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        NSLog(@"-----------=標記");
        
    }];
    
    // 毛玻璃效果
    deleteAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    
    editAction.backgroundColor = [UIColor blueColor];
    topAction.backgroundColor = [UIColor grayColor];
    signAction.backgroundColor = [UIColor yellowColor];
    
    return @[deleteAction, editAction, topAction, signAction];
}

 


 

能夠在導航欄右邊放編輯按鈕,刪除操做spa

-(void)delete:(UIBarButtonItem *)sender {
    
    if (self.tableView.editing == NO) {
        self.tableView.editing = YES;
        sender.title = @"完成";
    }else if (self.tableView.editing == YES) {
        self.tableView.editing = NO;
        sender.title = @"編輯";
    }
    
}

// 設置tableView是否能夠編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
// 設置刪除操做時候的標題
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"刪除";
}

 // 編輯模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.arrayM removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
    }
    
}

 

導航欄右邊放編輯按鈕,插入操做code

// 設置tableView是否能夠編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleInsert;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        // 咱們實現的是在所選行的位置插入一行,所以直接使用了參數indexPath
        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
        // 一樣,將數據加到list中,用的row
        [self.arrayM insertObject:@"新添加的行" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

 

 

導航欄右邊放編輯按鈕,移動操做orm

// 設置編輯模式
- (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];
    NSUInteger toRow = [destinationIndexPath row];
    
    id object = [self.arrayM objectAtIndex:fromRow];
    [self.arrayM removeObjectAtIndex:fromRow];
    [self.arrayM insertObject:object atIndex:toRow];
}
相關文章
相關標籤/搜索