簡體   English   中英

表格單元格上的按鈕不起作用-迅速

[英]Button on tableview cell is not working - swift

我的自定義表格視圖單元格上有一個按鈕,用於顯示用戶名。
當您單擊它時,您應該被帶到他/她的個人資料,但是什么也沒有發生?

這是我的自定義tableview單元格類(commentsTableViewCell.swift):

import UIKit

protocol commentsTableViewCellDelegate {
    func namesIsTapped(cell: commentsTableViewCell)
}


class commentsTableViewCell: UITableViewCell {

    @IBOutlet weak var nameButton: UIButton!
    @IBOutlet weak var commentLabel: UILabel!
    @IBOutlet weak var profilePic: UIImageView!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var uidLabel: UILabel!

    var delegate: commentsTableViewCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func nameIsTapped(sender: AnyObject) {
        //4. call delegate method
        //check delegate is not nil
        if let _ = delegate {
            delegate?.namesIsTapped(self)
        } else {
        print("Delegate is \(delegate)")
        }
    }
}

這是commentTableViewController.swift的重要部分:

class commentsTableViewController: UITableViewController, commentsTableViewCellDelegate, UITextViewDelegate {

    @IBOutlet weak var commentLabel: UITextView!
    @IBOutlet weak var theLabel: UILabel!

    var dataGotten = "Nothing"
    var updates = [CommentSweet]()

    func namesIsTapped(cell: commentsTableViewCell) {
        //Get the indexpath of cell where button was tapped
        //let indexPath = self.tableView.indexPathForCell(cell)

        let allDataSend = cell.nameButton.titleLabel?.text!
        self.performSegueWithIdentifier("toDetailtableViewController", sender: allDataSend)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "toDetailtableViewController" {
            if let nextVC = segue.destinationViewController as? theProfileTableViewController {
                nextVC.viaSegue = sender! as! String
            }
        }

    }

    override func viewDidLoad() {

請設置代表自我。

在cellforRowIndexPath中

commentsTableViewCell.delegate = self

其中commentsTableViewCell是自定義UITableViewCell對象

我建議在viewController中處理單元格按鈕的操作。 您可以通過為其設置tag來識別已點擊的按鈕。

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

        cell.myButton?.tag = indexPath.row
        cell.myButton?.addTarget(self, action: #selector(), for: .touchUpInside)

        return cell
    }

    func namesIsTapped(tappedButton: UIButton) {
        // get the user (from users array for example) by using the tag, for example:
        let currentUser = users[tappedButton.tag]

        // do whatever you want with this user now...

    }

暫無
暫無

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

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