2015-04-01 66 views

回答

0

您無法使用教程中描述的方法一次刪除多個單元。這隻適用於單細胞。如果您選擇多個單元格,並使用按鈕,例如,觸發刪除操作,你的代碼可能看起來是這樣的:

if let indexPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] { 
    for indexPath in indexPaths { 
     // one by one remove items from your datasource 
    } 

    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic) 
} 
1

而是在例如使用數[行]你可以使用數字[段] [中行]。所以代碼將如下所示:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return numbers[section].count 
    } 

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.Delete { 
     numbers[indexPath.section].removeAtIndex(indexPath.row)  
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
    } 
    } 
+0

感謝您的幫助。但我在運行應用程序時遇到異常,例外情況是: 由於未捕獲異常'NSInternalInconsistencyException',原因:'無效更新:第0節中的行數無效,因此終止應用程序。 update(1)必須等於更新前(1)的該部分中包含的行數,加上或減去從該部分插入或刪除的行數(0插入,1刪除)和正數或負數行移入或移出該部分(0移入,0移出)。' 爲什麼? – Rawan 2015-04-01 12:08:27

0

這兩個答案都不適用於我。 Swift數組索引在刪除時被更新,因此對於來自.indexPathsForSelectedRows()的索引,for-in循環提供了意外的結果,即刪除了錯誤的數據/表並最終導致數組邊界外的索引崩潰錯誤。發現好(但真的過時了)Objective-C iOS Developer Library example。但它利用了NSMutableArrayremoveObjectsAtIndexes方法,Swift數組不存在。無論如何,有很多有用的技巧值得一看。 對我來說工作的方法是該示例的一部分,但不是removeObjectsAtIndexesdo-while用於逐個刪除行,直到刪除所有選定的行。下面的方法由UIAlertAction調用,類似於Apple示例。

func deleteSelectedRows() { 

    // Unwrap indexPaths to check if rows are selected 
    if let _ = tableView.indexPathsForSelectedRows() { 
     // Do while all selected rows are deleted 
     do { 

     if let indexPath = tableView.indexPathForSelectedRow(){ 
     //remove from table view data source and table view 
     self.dataSource.removeAtIndex(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
     } 

     } while tableView.indexPathsForSelectedRows() != nil 

    }else{ 
     // Delete everything, delete the objects from data model. 
     self.dataSource.removeAll(keepCapacity: false) 

     // Tell the tableView that we deleted the objects. 
     // Because we are deleting all the rows, just reload the current table section 
     self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic) 
    } 
    // Exit editing mode after the deletion. 
    self.tableView.setEditing(false, animated: true) 
    } 

編輯:儘管它做了一個小例子我欺騙(絕對以swift開頭)的技巧它效率不高。要麼擴展數組,要麼使數據源等於並使用find()或.filter是可取的。但我確定應該有一個更簡單的方法。我現在使用的一個是在鏈接說明如下:

http://www.rockhoppertech.com/blog/swift-remove-array-item/

func == (lhs: myDataSource, rhs: myDataSource) -> Bool { 
    if lhs.data == rhs.data && 
    lhs.otherData == rhs.otherData { 
     return true 
    } 
    return false 
} 

struct myDataSource: Equatable { 
let data: String 
let otherData: String 
} 

然後:

if let selectedRows = tableView.indexPathsForSelectedRows(){ 
    var objectsToDelete = [myDataSource]() 
    for selectedRow in selectedRows { 
    objectsToDelete.append(myDataSource[selectedRow.row]) 
    } 
    for object in objectsToDelete { 
    if let index = find(myDataSource, object){ 
     myDataSource.removeAtIndex(index) 
    } 
    } 
} 
tableView.deleteRowsAtIndexPaths([selectedRows], withRowAnimation: .Automatic) 
0

嘗試這個。這工作正常。 但是不要忘了這之前。 FUNC的tableView(的tableView:UITableView的,canEditRowAtIndexPath indexPath:NSIndexPath) - >布爾 { 還真 }

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == .Delete 
    { 
     arrayOfnames.removeAtIndex(indexPath.row) 
     self.tableViewww.reloadData() 
    } 
} 
相關問題