2015-07-27 2 views
2

Я пытаюсь удалить удаленные ячейки из своего табличного представления, и каждый раз, когда я пытаюсь сделать это, я получаю сообщение об ошибке.удаление строк и разделов из UITableView swift

Это мой код для фактического удаления

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     // Delete the row from the data source 
     self.tableView.beginUpdates() 
     self.feedData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists. 
     self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 1)) as [AnyObject], withRowAnimation: UITableViewRowAnimation.Left) 
     // self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 1)) as [AnyObject], withRowAnimation: UITableViewRowAnimation.Left) 
     self.tableView.endUpdates() 
    } else if editingStyle == .Insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    }  
} 

И это то, что я возвращаюсь в моих разделах

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

Я новичок так что медведь со мной :)

+0

Какая у вас ошибка? – pbush25

ответ

1

Для обработки удаления ударов необходимо установить следующие две функции:

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

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) { 
    if (editingStyle == .Delete) { 
    // handle delete (by removing the data from your array and updating the tableview) 

    self.tableView.beginUpdates() 

    self.feedData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists. 
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left) 

    self.tableView.endUpdates() 
} 

Функция tableView(_:canEditRowAtIndexPath:) в соответствии с Apple:

Допускает источник данных, чтобы исключить отдельные строки из рассматривается как редактируемые. Редактируемые строки отображают управление вставкой или удалением в своих ячейках. Если этот метод не реализован, все строки предполагается редактируемыми. Строки, которые не редактируются, игнорируют свойство editingStyle объекта UITableViewCell и не имеют отступов для элемента управления удалением или вставки. Строки, доступные для редактирования, но не требующие отображения вставки или удаления, могут возвращать UITableViewCellEditingStyleNone из метода делегата tableView:editingStyleForRowAtIndexPath:.

Я надеюсь, что это поможет вам.

+0

спасибо, что помог –

+0

@AbhishekMahesh Я рад помочь. Пожалуйста, если ответ решит ваш вопрос, примите его, чтобы помочь другому в будущем. –

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