2017-05-14 85 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) 
}