2017-07-30 111 views
0

我正在爲實踐項目創建在線訂購服務。我最近讀到的兩件事是編輯tableview函數。當我去滑動刪除一個項目時,它不會從數據庫中取出正確的項目。另外,如果數組中只有一個項目,我試圖刪除它,它會使它崩潰。任何人都知道這是怎麼回事?從Firebase數據庫中刪除

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

    orderNumber.remove(at: indexPath.row) 
    itemsArray.remove(at: indexPath.row) 
    priceArray.remove(at: indexPath.row) 
    quantityArray.remove(at: indexPath.row) 

    databaseRef.child("Users/\((authRef.currentUser?.uid)!)/Order/\(orderNumber.remove(at: indexPath.row))").removeValue { (error, ref) in 
     if error != nil { 
      print(error?.localizedDescription as Any) 
     } else { 

     } 
    } 
} 

回答

0

看起來像你只需要刪除它之前檢查數組數: -

if orderNumber.count > indexPath.row { 
orderNumber.remove(at: indexPath.row) 
} 
if itemsArray.count > indexPath.row { 
itemsArray.remove(at: indexPath.row) 
} 
if priceArray.count > indexPath.row { 
priceArray.remove(at: indexPath.row) 
} 
if quantityArray.count > indexPath.row { 
quantityArray.remove(at: indexPath.row) 
} 
+0

它仍然崩潰。問題發生在數據庫引用調用中。我在調試器中得到的唯一結果是「索引超出範圍」 – user3103854

0

顯然,你從orderNumber除去兩次。首先,你要刪除的位置:

orderNumber.remove(at: indexPath.row) 

然後你刪除在這裏:

child("Users/\((authRef.currentUser?.uid)!)/Order/\(orderNumber.remove(at: indexPath.row))") 

你使用多個集合任何具體的原因是什麼?

+0

是的,我注意到並修復了它。我解決了另一個問題,但現在當我刪除一個時,它將複製並使用這些選項重新填充表視圖以及舊的。 – user3103854

+0

請粘貼您的當前代碼。 –