我有一個表視圖,所有單元格的編輯樣式都是.delete。但我想讓一些特定的單元格不具有編輯樣式(這樣你就不能滑動它們,.none)。有人對如何實作這一目標有什么建議嗎?
我試著為特定的行寫一些類似UITableViewCell.EditingStyle.none的東西,但這并不奏效。
預先感謝,Pontus
uj5u.com熱心網友回復:
考慮一下下面的例子:
class ViewController。UITableViewController {
private let arr =(1。 ...10).map { "Item ($0)" }
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView。UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell"/span>)
cell?.textLabel?.text = arr[indexPath.row] 。
return cell ? UITableViewCell()
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if indexPath.row == 2 {//<- Your condition.
return .none
} else {
return .delete
}
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true {
}
}
因此,關鍵是使用editingStyleForRowAt來改變特定單元格(甚至是多個單元格)的樣式(進一步參考檔案。https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614869-tableview)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/311737.html
標籤:
上一篇:分隔線UITableView
