繁体   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