[英]UITableView disable swipe to delete for particular cells swift
我正在嘗試禁用滑動以刪除 tableview 中某些特定單元格的操作。 但我無法迅速找到一個好的解決方案。 當覆蓋“tableView.isEditing = true”方法時,我不希望單元格上的左側減號。這是我的代碼。 下面的代碼沒有禁止滑動到 statusId 為“12”的單元格。 希望你明白我的問題。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! TableViewDateCell
cell.titleStatus.text = mainList[indexPath.row].statusName
//tableView.isEditing = true
//disable swipe to delete for cell with statusId == "12"
if mainList[indexPath.row].statusId == "12" {
//tableView.isEditing = false
cell.selectionStyle = UITableViewCellSelectionStyle.gray
cell.isUserInteractionEnabled = false
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
您可以自定義UITableViewDelegate
的函數editingStyleForRowAt
,尤其是在不需要滑動時返回UITableViewCellEditingStyle.none
,例如:
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
if mainList[indexPath.row].statusId == "12" {
return UITableViewCellEditingStyle.none
} else {
return UITableViewCellEditingStyle.delete
}
}
使用這個表視圖委托方法
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
}
感謝您發布這個問題和上面的答案。
在答案的擴展中,我有一個永遠不應刪除的默認行,因此我在該特定單元格上設置了一個標簽,然后首先使用了此方法。
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if tableView.cellForRow(at: indexPath)?.tag == 100 {
return false
}
return true
}
但是- 這導致調試器中出現以下警告 -
嘗試在表視圖上調用 -cellForRowAtIndexPath: 在更新其可見單元格的過程中,這是不允許的。
因此,正如其他評論員所說,請使用以下方法:
斯威夫特 5.0
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if tableView.cellForRow(at: indexPath)?.tag == 100 {
return UITableViewCell.EditingStyle.none
} else {
return UITableViewCell.EditingStyle.delete
}
}
我在編輯 myTableview 時試圖禁用/啟用刪除功能。 這是我的解決方案:
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
{
if !myTableView.isEditing {
return UITableViewCell.EditingStyle.none
} else {
return UITableViewCell.EditingStyle.delete
}
}
viewDidLoad 中的 Swift 4.2 只需將您的表值設置為...
myTableView.isEditing = false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.