以编程方式从单元格内的UI按钮执行segue到新的View Controller? (没有故事板)

时间:2017-07-04 12:28:54

标签: swift xcode uitableview uistoryboardsegue

我想通过单击在单元格中找到的UIButton(用户单元格)来执行对新VC的segue

class TableViewController: UITableViewController {

    var userScreenVC: usersScreenVC?
    var HomePageVc: HomePageVC?
    var signInVC: SignInVC?

    var ref = FIRDatabase.database().reference()

    let cellId = "cellId"
    var users = [User]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UserCellLargePicture.self, forCellReuseIdentifier: cellId)
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCellLargePicture

        cell.selectionStyle = .none
        cell.preservesSuperviewLayoutMargins = false
        cell.separatorInset = UIEdgeInsets.zero
        cell.layoutMargins = UIEdgeInsets.zero

        let user = users[indexPath.row]

        cell.ProfileButton.setImage(#imageLiteral(resourceName: "Grape"), for: .normal)

        return cell

    }//func tableview cellForRowAt

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 234
    }

class UserCellLargePicture: UITableViewCell {

    var ProfileButton: UIButton = {
        let button = UIButton()
        //label.text = "TEST TEST TEST"
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)

        addSubview(ProfileButton)

        ProfileButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -58).isActive = true
        ProfileButton.widthAnchor.constraint(equalToConstant: 54).isActive = true
        ProfileButton.heightAnchor.constraint(equalToConstant: 54).isActive = true
        ProfileButton.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant:0).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

0 个答案:

没有答案