2017-05-14 81 views
0

如何通過,如果做一個去除靜態表視圖單元格聲明如下:刪除表格視圖單元格與if語句


如果____ {

刪除靜態細胞

}其他{

保持細胞中的tableview

}


加粗的部分是什麼,我需要的代碼。我在網上搜索了一個答案,但我找不到一個答案。謝謝您的幫助!我正在使用Swift 3

+0

嚴格地說,一個***靜態***單元格應該在Interface Builder中進行硬編碼並且無法刪除。 – vadian

回答

0

首先,確保將單元格更改爲具有動態屬性,因爲靜態單元格是硬編碼的。 其次,你不需要一個else語句。如果條件成立,請刪除該單元格,否則不做任何操作。要刪除單元格,請使用以下函數:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
    print("Cell deleted") 
    // delete any additional data from containers like arrays or dictionaries if needed 
    self.tableView.deleteRows(at: [indexPath], with: .automatic) 
    } 
} 
+0

@NedimKurbeggović謝謝!快速問題,我在哪裏設置要刪除哪個單元格?我在代碼中找不到它 – iFunnyVlogger

0

如果這是一個靜態tableview,則無法刪除單元格。如果你嘗試着,你可能會陷入各種各樣的問題。您最好的解決方案是將單元格的高度設置爲零並隱藏它。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = super.tableView(tableView, cellForRowAt:indexPath) 
    if indexPath == cellToHide { 
     cell.isHidden = true 
    } 
    return cell 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath == cellToHide { 
     return 0 
    } 
    return super.tableView(tableView, heightForRowAt: indexPath) 
}