0
我有一個UITableView
與UITableViewCell
s其accessoryView
是UIButton
s。當用戶按下這個UIButton
時,會觸發一個動作。它工作的很好,但有一個不方便:accessoryView
沒有卡在單元格的右邊緣,所以當用戶按下單元格的最右側時,按鈕不會被觸發,甚至更糟糕:單元格被選中,觸發不想要的行爲。如何在UITableViewCell的某個位置禁用用戶交互?
這是我如何定義我的細胞:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier")
let accessoryButton = MyCustomAccessoryView(frame: CGRect(x: 0.0, y: 0.0, width: 45.0, height: 45.0))
accessoryButton.addTarget(self, action: #selector(MyController.accessoryButtonTapped(_:)), for: .touchUpInside)
cell?.accessoryView = accessoryButton
return cell
}
MyCustomAccessoryView.swift:
class MyCustomAccessoryView: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
下面是結果:
所以我的問題是:有一種方法可以禁用012最右側的用戶交互?
謝謝,但我試圖添加按鈕,我的單元格的內容的子視圖,並有按鈕和電池(幾乎是不可能的觸摸按鈕,即使'gestureRecognizer之間的巨大沖突:shouldRequireFailureOf:'方法來實現,該細胞是點擊按鈕時選擇)。 我沒有得到你的第二個解決方案:如果我在右側放置一個空白視圖,點擊這個視圖將選擇單元格,對不對? – Randy