2015-06-06 3 views
2

Я хочу, чтобы пользователь мог удалять ячейку таблицы в настоящий момент, когда у меня есть действие удаления, но я не знаю, как на самом деле удалить выбранную ячейку! Как вы можете сказать, что я имею в программировании в Свифта и Xcode 6.3.1UITableViewRowAction: Удалить

var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in 
      tableView.editing = false 
      println("deleteAction") 
      } 

Вот скриншот на раздаточной, а также: Screenshot Of Delete Swipe Action

ответ

1

Если вы хотите, чтобы полностью удалить ячейку из Tableview, то вы должны удалить его из таблицы массива слишком , Рассмотрим ниже код:

var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in 
     tableView.editing = true 
      // Remove it from your TableArray and If it is stored into any local storage then you have to remove it from there too because if you doesn't remove it from your local storage then when you reload your tableview it will appears back 

      self.tableData.removeAtIndex(indexPath.row) 

      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 

    } 

    return [deleteAction] 
} 
+0

Это работало, у меня были некоторые ошибки с моим массивом, но это было из-за того, что это было не пустым! Спасибо хоть!! –

1

Это может помочь вам.

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) { 
    if (editingStyle == UITableViewCellEditingStyle.Delete) { 
     // handle delete (by removing the data from your array and updating the tableview) 
     if let tv=tableView 
     { 
     items.removeAtIndex(indexPath!.row) 
      tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

    } 
} 
+0

Когда я добавляю его в совершение редактирования стиль и добавить свой собственный массив элементов (что «продукты») я получаю ошибку «неизменяемое значение типа '[String] только имеет мутирует члены, названные 'removeAtIndex' '@RandomBytes –

+0

, попробуйте, что предложил Dharmesh Kheni, это может быть решение. Я слишком похмелье, чтобы действительно помочь намного больше, чем это. Удачи. – RandomBytes

1

Вы должны реализовать editActionsForRowAtIndexPath и commitEditingStyle

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    __weak SampleViewController *weakSelf = self; 

    UITableViewRowAction *actionRed = 
    [UITableViewRowAction 
    rowActionWithStyle:UITableViewRowActionStyleNormal 
    title:@"Delete" 
    handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { 
     NSLog(@"Delete!"); 

     [weakSelf.itemsList removeObjectAtIndex:indexPath.row]; 
     [weakSelf.tableView setEditing:NO animated:YES]; 
     [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] 
            withRowAnimation:UITableViewRowAnimationAutomatic]; 

    }]; 

    actionRed.backgroundColor = [UIColor colorWithRed:0.844 green:0.242 blue:0.292 alpha:1.000]; 


    return @[actionRed]; 
} 


/* 
* Must implement this method to make 'UITableViewRowAction' work. 
* 
*/ 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 

} 

Вот пример ActionRowTest

Смежные вопросы