簡體   English   中英

Swift 3.0 TableView單元格按鈕

[英]Swift 3.0 TableView Cell Button

我在tableview單元格上設置了一個按鈕,我想知道如何操作它。 也就是說,我有興趣更改按鈕的名稱標題,並在單擊按鈕時添加另一個目標(按鈕的不同功能)。 我的想法是需要將它添加到buttonClicked func中,但我不確定如何引用被單擊的特定cellForRow。 或許可以在cellForRow中使用條件來確定按鈕標題是什么? 我不太清楚最好的辦法是什么。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = guestTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

    let button : UIButton = UIButton(type:UIButtonType.custom) as UIButton

    button.frame = CGRect(origin: CGPoint(x: 200,y :60), size: CGSize(width: 100, height: 24))
    let cellHeight: CGFloat = 44.0
    button.center = CGPoint(x: view.bounds.width / (4/3), y: cellHeight / 2.0)


    button.setTitleColor(.blue, for: .normal)
    button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
    button.setTitle("Add", for: UIControlState.normal)

    cell.addSubview(button)


    return cell
}

func buttonClicked(sender : UIButton!) {
    print("Added!")


}

對於Swift 3.2和4.0

找到“ indexPath.row ”和“ indexPath.section

//在“cellForRowAt”按鈕上添加目標

cell.myBtn.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)

//添加功能

func btnAction(_ sender: UIButton) {

    let point = sender.convert(CGPoint.zero, to: yourTableView as UIView)
    let indexPath: IndexPath! = yourTableView.indexPathForRow(at: point)

    print("row is = \(indexPath.row) && section is = \(indexPath.section)")
}

Chnage並添加您的代碼

    button.addTarget(self, action:#selector(YourViewController.buttonClicked(_:)), for: .touchUpInside)

    button.tag = indexPath.row



func buttonClicked(sender : UIButton!) {
    print("Added!")
     let row = sender.tag;
    print(row)

}

有兩種方法可以做到 - 欺騙的方式或正確的方式。 正確的是子類UITableViewCell,在這種情況下,你可以正常方式連接出口。 作弊的方法是使用每個元素的標記屬性。 如果將按鈕的標記設置為(例如)1,則可以使用cellForRowAt cell.viewWithTag來定位它:如下所示:

let button = cell.viewWithTag(1) as! UIButton

請注意,它將搜索cell下方的整個層次結構,因此您需要確保沒有多個具有相同標記的子視圖。

從你的問題,我猜你想要點擊按鈕並更改其標題。 click處理程序應該只重新加載tableView行(如果你像我一樣懶,那么整個表),而cellForRowAt需要知道要顯示的標題。 每個按鈕上顯示的內容必須保存在tableView單元格之外,因為在滾動時會重復使用單元格。 這意味着一旦頂行滾動屏幕,相同的單元格可能會被重復用於(例如)第20行。 cellForRowAt的代碼需要完全重置單元格的內容,以防它已被用於另一行。

從你對這個問題的評論中添加一些細節,你永遠不想避免重復使用單元格,因為這是一件非常好的事情。 您可能正在顯示一個包含1,000行的表,但在屏幕上只能顯示10行。 重用單元格意味着iOS可以創建少至10個單元格來顯示整個表格,而不是1000。 結果表現更快。

Swift 4.x中 ,您可以簡單地讓TableView的委托為其設置標記,如下所示:

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

    // Assign tags
    cell.myButton.tag = indexPath.row

    return cell
}

然后通過右鍵單擊+拖動到.swift文件作為@IBAction來連接故事板中的按鈕,選擇Type - > UIButton並單擊Connect

這是代碼:

 @IBAction func myButt(_ sender: UIButton) {
    let buttTag = sender.tag // get the Tag of the clicked button in the Cell

    // write the code that will be called on click of your button 
 }

暫無
暫無

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

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