簡體   English   中英

自定義附件視圖按鈕的操作是什么 - swift

[英]What is the action for custom accessory view button - swift

這似乎在 swift 和 objc 中被問過幾次,但我看不到 swift 的正確答案,所以希望這次有人能幫助我。 我創建了一個自定義附件視圖按鈕,但需要正確的按鈕操作:因為“accessoryButtonTapped”是一個無法識別的選擇器。

調用 tableViewCellDelegate 方法 willSelectRowAtIndexPath 所需的選擇器是什么?

我的代碼是:

let cellAudioButton = UIButton(type: .Custom)
cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
cellAudioButton.addTarget(self, action: "accessoryButtonTapped", forControlEvents: .TouchUpInside) //INCORRECT ACTION:
cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
cellAudioButton.contentMode = .ScaleAspectFit
cell.accessoryView = cellAudioButton as UIView

提前感謝任何可以提供幫助的人。

為什么需要調用willSelectRowAtIndexPath 你已經做對了一切,為了解決你unrecognized selector錯誤,只需創建一個在你點擊cell.accessoryView時調用的cell.accessoryView 在你的情況下:

func accessoryButtonTapped(){
    print("Tapped")
}

更新
如果你想獲得 indexPath 你可以

給你的cellAudioButton添加一個標簽:

cellAudioButton.tag = indexPath.row

在你的addTarget添加一個:來傳遞一個參數

在你的功能中

func accessoryButtonTapped(sender : AnyObject){
        print(sender.tag)
        print("Tapped")
    }

所以整個代碼:

let cellAudioButton = UIButton(type: .Custom)
        cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        cellAudioButton.addTarget(self, action: "accessoryButtonTapped:", forControlEvents: .TouchUpInside)
        cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
        cellAudioButton.contentMode = .ScaleAspectFit
        cellAudioButton.tag = indexPath.row
        cell.accessoryView = cellAudioButton as UIView

func accessoryButtonTapped(sender : AnyObject){
        print(sender.tag)
        print("Tapped")
    }

Swift 3.1更新Rashwan解決方案

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
       //add button as accessory view
        let cellAudioButton = UIButton(type: .custom)
        cellAudioButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        cellAudioButton.addTarget(self, action: #selector(ViewController.accessoryButtonTapped(sender:)), for: .touchUpInside)
        cellAudioButton.setImage(UIImage(named: "closeRed"), for: .normal)
        cellAudioButton.contentMode = .scaleAspectFit
        cellAudioButton.tag = indexPath.row
        cell.accessoryView = cellAudioButton as UIView

        return cell
    }
func accessoryButtonTapped(sender : UIButton){
        print(sender.tag)
        print("Tapped")
    }

筆記:

這里ViewController的名稱,如LoginViewController

為什么不使用UITableViewDelegate

@available(iOS 2.0, *)
optional func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath)

為了澄清這一點,但這是可能的。 您可以將自定義 UI 元素用作附件按鈕。

這是一個示例——假設您使用的是 Interface Builder。 UISwitch添加到自定義單元格。 現在右鍵單擊從UITableViewCell拖放至UISwitch 選擇accessoryView作為出口連接。 完畢。

每次按下開關按鈕時,都會觸發委托方法。 這也應該適用於其他操作元素,例如UIButton s。 這正是 OP 要求的用例。

暫無
暫無

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

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