UITableViewCell, показать кнопку удаления при прокрутке

512

Как получить кнопку удаления, отображаемую при прокрутке на UITableViewCell? Событие никогда не поднимается, и кнопка удаления никогда не появляется.

  • 1
    Если вам нужен подробный и обновленный ответ, перейдите к stackoverflow.com/a/37719543/6872794.
  • 0
    Смотрите мой ответ Swift 4 для аналогичного вопроса, который показывает до 3 различных способов создания свайп для удаления действий для UITableViewCell s.
Показать ещё 1 комментарий
Теги:
uitableview
uikit
cocoa-touch

16 ответов

927
Лучший ответ
// During startup (-viewDidLoad or in storyboard) do:
self.tableView.allowsMultipleSelectionDuringEditing = NO;


// Override to support conditional editing of the table view.
// This only needs to be implemented if you are going to be returning NO
// for some items. By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }    
}
  • 92
    Это работает, но ... - (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath ... необходимо реализовать только в том случае, если вы собираетесь возвращать NO для некоторых элементов. По умолчанию все элементы доступны для редактирования, поэтому вам не нужно реализовывать их, если вы всегда возвращаете YES.
  • 24
    Также важно знать: это методы UITableViewDataSource и NOT UITableViewDelegate.
Показать ещё 6 комментариев
67

Этот ответ был обновлен до Swift 3

Я всегда думаю, что хорошо иметь очень простой, самодостаточный пример, так что ничего не предполагается, когда я изучаю новую задачу. Этот ответ заключается в том, что для удаления строк UITableView. Проект выполняется следующим образом:

Изображение 5317

Этот проект основан на примере UITableView для Swift.

Добавить код

Создайте новый проект и замените код ViewController.swift следующим.

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // These strings will be the data for the table view cells
    var animals: [String] = ["Horse", "Cow", "Camel", "Pig", "Sheep", "Goat"]

    let cellReuseIdentifier = "cell"

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // It is possible to do the following three things in the Interface Builder
        // rather than in code if you prefer.
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
    }

    // number of rows in table view
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.animals.count
    }

    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

        cell.textLabel?.text = self.animals[indexPath.row]

        return cell
    }

    // method to run when table view cell is tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped cell number \(indexPath.row).")
    }

    // this method handles row deletion
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {

            // remove the item from the data model
            animals.remove(at: indexPath.row)

            // delete the table view row
            tableView.deleteRows(at: [indexPath], with: .fade)

        } else if editingStyle == .insert {
            // Not used in our example, but if you were adding a new row, this is where you would do it.
        }
    }

}

Единственный ключевой метод в приведенном выше коде, который включает удаление строк, является последним. Здесь снова для акцента:

// this method handles row deletion
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {

        // remove the item from the data model
        animals.remove(at: indexPath.row)

        // delete the table view row
        tableView.deleteRows(at: [indexPath], with: .fade)

    } else if editingStyle == .insert {
        // Not used in our example, but if you were adding a new row, this is where you would do it.
    }
}

Раскадровка

Добавьте UITableView в контроллер просмотра в раскадровке. Используйте автоматический макет, чтобы прикрепить четыре стороны таблицы к краям View View. Управляйте перетаскиванием из представления таблицы в раскадровке в строку @IBOutlet var tableView: UITableView! в коде.

Пройденные

Это все. Теперь вы можете запустить свое приложение и удалить строки, щелкнув влево и нажав "Удалить".


Варианты

Измените текст кнопки "Удалить"

Изображение 5318

Добавьте следующий метод:

func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
    return "Erase"
}

Пользовательские действия кнопок

Изображение 5319

Добавьте следующий метод.

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    // action one
    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in
        print("Edit tapped")
    })
    editAction.backgroundColor = UIColor.blue

    // action two
    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) in
        print("Delete tapped")
    })
    deleteAction.backgroundColor = UIColor.red

    return [editAction, deleteAction]
}

Обратите внимание, что это доступно только для iOS 8. Подробнее см. этот ответ.

Обновлен для iOS 11

Действия могут быть помещены как ведущие, так и конечные элементы ячейки, используя методы, добавленные в API UITableViewDelegate в iOS 11.

func tableView(_ tableView: UITableView,
                leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
 {
     let editAction = UIContextualAction(style: .normal, title:  "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
             success(true)
         })
editAction.backgroundColor = .blue

         return UISwipeActionsConfiguration(actions: [editAction])
 }

 func tableView(_ tableView: UITableView,
                trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
 {
     let deleteAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
         success(true)
     })
     deleteAction.backgroundColor = .red

     return UISwipeActionsConfiguration(actions: [deleteAction])
 }

Дальнейшее чтение

  • 0
    спасибо за примеры и код. Теперь я готов реализовать функцию удаления. Подскажите, пожалуйста, какова цель строки "self.tableView.registerClass (...", которую вы добавили в viewDidLoad ()? И что эквивалентно этому в построителе интерфейса? Этого не было в примере с пользовательской ячейкой. Похоже, мы сейчас указываем cellReuseIdentifier дважды. Спасибо!
  • 0
    Если включить строку .registerClass, компиляция завершится неудачно
Показать ещё 8 комментариев
63

В этом коде показано, как реализовать удаление.

#pragma mark - UITableViewDataSource

// Swipe to delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_chats removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

При необходимости в переопределении инициализации добавьте строку ниже, чтобы отобразить элемент "Изменить":

self.navigationItem.leftBarButtonItem = self.editButtonItem;
  • 0
    это показывает неопределенную ошибку _chats, как я могу решить это? Спасибо!
  • 0
    Вы должны реализовать этот метод. Содержимое внутри должно соответствовать тому, что имеет смысл для вашего варианта использования. В приведенном выше коде _chats - это данные поддержки для табличного представления. Как только пользователь нажимает «удалить», отдельный объект чата должен быть удален из _chat, чтобы источник данных отражал количество новых строк (в противном случае выбрасывается исключение).
31

Примечание. У меня недостаточно репутации, чтобы опубликовать комментарий в ответе из Курбза.

Ответ от Курбса прав. Но для меня это никогда не срабатывало.

После некоторого расследования я понял, что прокрутка к удалению происходит, когда НЕ редактирует представление таблицы..

Я никогда не видел, чтобы это прямо указывалось как таковое. Если я не ошибаюсь, я не нашел другого способа заставить его работать.

Когда вы редактируете, появится элемент управления delete и /or reorder.

21

У меня была проблема, которую мне только удалось решить, поэтому я делюсь ею, поскольку она может помочь кому-то.

У меня есть UITableView и добавлены методы, показанные для удаления удаляемого файла:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }    
}

Я работаю над обновлением, которое позволяет мне перевести таблицу в режим редактирования и включить multiselect. Для этого я добавил код из Apple TableMultiSelect. Как только я получил эту работу, я обнаружил, что моя функция удаления перестала работать.

Оказывается, что добавление следующей строки в viewDidLoad было проблемой:

self.tableView.allowsMultipleSelectionDuringEditing = YES;

С помощью этой строки многосегмент будет работать, но удалять салфетки не удастся. Без линии это было наоборот.

Исправление:

Добавьте в свой viewController следующий метод:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    self.tableView.allowsMultipleSelectionDuringEditing = editing; 
    [super setEditing:editing animated:animated];
}

Затем в вашем методе, который помещает таблицу в режим редактирования (например, нажатием кнопки), вы должны использовать:

[self setEditing:YES animated:YES];

вместо:

[self.tableView setEditing:YES animated:YES];

Это означает, что multiselect активируется только тогда, когда таблица находится в режиме редактирования.

  • 0
    Это было полезно. Я установил allowMultipleSelection в раскадровке. Это исправило это.
  • 1
    Это решило проблему, которая сводила нас с ума. Теперь я понимаю, что «смахивание для удаления» и «пакетное удаление в режиме редактирования» в основном являются взаимоисключающими, и вы должны контролировать это при входе в режим редактирования / ухода. Большое спасибо за исследование этого!
16

В iOS 8 и Swift 2.0 попробуйте это,

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
   // let the controller to know that able to edit tableView row 
   return true
}

override func tableView(tableView: UITableView, commitEdittingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)  {
   // if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?  {
   // add the action button you want to show when swiping on tableView cell , in this case add the delete button.
   let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in

   // Your delete code here.....
   .........
   .........
   })

   // You can set its properties like normal button
   deleteAction.backgroundColor = UIColor.redColor()

   return [deleteAction]
}
  • 0
    Это хороший ответ, с его помощью вы также можете настроить несколько действий.
16

Ниже UITableViewDataSource поможет вам удалить удаленный

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [arrYears removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }
}

arrYears - это NSMutableArray, а затем перезагружает tableView

Swift

 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return true
        }

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyleDelete {
        arrYears.removeObjectAtIndex(indexPath.row)
        tableView.reloadData()
    }
}
  • 0
    Но это UITableViewDataSource
9

@Курбз ответ потрясающий, но я хочу оставить эту заметку и надеюсь, что этот ответ может спасти людей некоторое время.

Я иногда имел эти строки в своем контроллере, и они не работали с функцией очистки.

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

Если вы используете UITableViewCellEditingStyleInsert или UITableViewCellEditingStyleNone в качестве стиля редактирования, функция прокрутки не работает. Вы можете использовать только UITableViewCellEditingStyleDelete, который является стилем по умолчанию.

  • 1
    В моем случае я хотел иметь возможность провести удаление, но также и иметь возможность перемещать свои ячейки. Подвижная ячейка также получает эту кнопку «удалить» в левой части ячейки, которая не вписывалась в мой дизайн, и чтобы удалить это, стиль редактирования должен быть .none. Я решил это, "если tableView.isEditing {return .none} else {return .delete}"
  • 0
    Спас мой Axz чувак. Спасибо :)
7

Swift 3

Все, что вам нужно сделать, это включить следующие две функции:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

    return true

}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete {
        tableView.reloadData()
    }

}
7

Кроме того, это может быть достигнуто в SWIFT с использованием метода следующим образом

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete){
        testArray.removeAtIndex(indexPath.row)
        goalsTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}
5

Я знаю, старый вопрос, но @Kurbz ответ просто нужно это для Xcode 6.3.2 и SDK 8.3

Мне нужно добавить [tableView beginUpdates] и [tableView endUpdates] (спасибо @bay.phillips здесь)

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // Open "Transaction"
    [tableView beginUpdates];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // your code goes here
        //add code here for when you hit delete
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
     }

    // Close "Transaction"
    [tableView endUpdates];
}
5
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //add code here for when you hit delete
        [dataSourceArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }    
}    
  • 0
    dataSourceArray - массив, из которого поступает содержимое ячейки.
5

Когда вы удаляете ячейку своего табличного представления, вам также нужно удалить объект массива с индексом x.

Я думаю, вы можете удалить его, используя жестом салфетки. Вид таблицы вызовет делегата:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        [dataSourceArray removeObjectAtIndex:indexPath.row];
    }    
}

После удаления объекта. Вы должны перезагрузить использование таблицы. Добавьте в код следующую строку:

[tableView reloadData];

после этого вы успешно удалили строку. И когда вы перезагружаете представление или добавляете данные в DataSource, объект больше не будет там.

Для всех остальных ответ от Курбза правильный.

Я только хотел напомнить вам, что функции делегата будет недостаточно, если вы хотите удалить объект из массива DataSource.

Надеюсь, я помог тебе.

2

Swift 4

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in
        // delete item at indexPath
    tableView.deleteRows(at: [indexPath], with: .fade)

    }
    return [delete]
}
  • 1
    Хорошо, появляется вкладка удаления, но не удаляется при нажатии. Вам нужно удалить объект в источнике данных и перезагрузить таблицу да?
  • 0
    да "// удалить элемент в indexPath" разместить логику вашей строки удаления на основе indexPath
1

Для Swift просто напишите этот код

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            print("Delete Hit")
        }
}

Для Objective C просто напишите этот код

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       if (editingStyle == UITableViewCellEditingStyleDelete) {           
            NSLog(@"index: %@",indexPath.row);
           }
}
1

Swift 2.2:

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func tableView(tableView: UITableView,
    editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "DELETE"){(UITableViewRowAction,NSIndexPath) -> Void in

    print("Your action when user pressed delete")
}
let edit = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "EDIT"){(UITableViewRowAction,NSIndexPath) -> Void in

    print("Your action when user pressed edit")
}
    return [delete, block]
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню