簡體   English   中英

希望附件選中標記僅在右側點擊時顯示

[英]Want accessory checkmark to show only when tapped on the right

我有一個帶有單元格的TableView,當在單元格中的任何位置按該按鈕時,它將在右側添加一個選中標記。 我只希望在右側輕按單元格時顯示選中標記。 這是TableViewController中相關的代碼部分:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell

        let task = tasks[indexPath.row]
            cell.task = task


        if task.completed {
            cell.accessoryType = UITableViewCellAccessoryType.checkmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryType.none;
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)

        var tappedItem = tasks[indexPath.row] as Task
        tappedItem.completed = !tappedItem.completed
        tasks[indexPath.row] = tappedItem

        tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
    }

}

有沒有簡單的方法可以做到這一點,或使用情節提要來做到這一點? 我的Swift技能讓很多人望而卻步。 任何幫助,將不勝感激! 謝謝!

為什么不提供內置的復選標記附件類型,而不是提供用戶可以點擊並顯示復選標記的實際按鈕作為附件視圖? 例如,當用戶點擊按鈕時,該按鈕可能會正常顯示為一個空圓圈,並顯示為帶有選中標記的圓圈。

否則,您期望用戶猜測一個晦澀的界面,而通過這種方式,您可以很明顯地點擊此處將任務標記為已完成。

例:

在此處輸入圖片說明

為了實現這個目標,我創建了一個按鈕子類,並設置accessoryView每個細胞,以它的一個實例:

class CheckButton : UIButton {
    convenience init() {
        self.init(frame:CGRect.init(x: 0, y: 0, width: 20, height: 20))
        self.layer.borderWidth = 2
        self.layer.cornerRadius = 10
        self.titleLabel?.font = UIFont(name:"Georgia", size:10)
        self.setTitleColor(.black, for: .normal)
        self.check(false)
    }
    func check(_ yn:Bool) {
        self.setTitle(yn ? "✔" : "", for: .normal)
    }
    override init(frame:CGRect) {
        super.init(frame:frame)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

按鈕的標題可以是空字符串或復選標記字符,從而使您在輕按按鈕時可以看到效果。 這段代碼來自cellForRowAt: ::

    if cell.accessoryView == nil {
        let cb = CheckButton()
        cb.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        cell.accessoryView = cb
    }
    let cb = cell.accessoryView as! CheckButton
    cb.check(self.rowChecked[indexPath.row])

(其中rowChecked是Bool的數組)。

您將必須定義自己的附件按鈕,並處理其自己的單擊。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell

    let task = tasks[indexPath.row]
        cell.task = task

    let checkButton = UIButtonSubclass()
    ...configure button with your circle and check images and a 'selected property'...
    checkButton.addTarget(self, action:#selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
    cell.accessoryView = checkButton
    checkButton.selected = task.completed //... this should toggle its state...

    return cell
}

func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
    guard let touch = event.allTouches?.first else { return }
    let point = touch.location(in: self.tableview)
    let indexPath = self.tableview.indexPathForRow(at: point)
    if let task = tasks[indexPath.row] {
        task.completed = !task.completed
    }
    tableView.reloadData()   //could also just reload the row you tapped
}

但是,已經注意到,如果您開始刪除行,則使用標記檢測被點擊的行是危險的。 您可以在這里閱讀更多內容https://stackoverflow.com/a/9274863/1189470

編輯刪除了每個@matt標記的引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM