簡體   English   中英

如何執行從自定義UITableViewCell(xib)到另一個ViewController的Segue視圖

[英]How to perform a segue view from custom UITableViewCell(xib) to another ViewController

我想在我的自定義UITableViewCell上顯示一個按鈕,將用戶帶到另一個屏幕上單擊。 我試過下面的代碼,但是不起作用

子視圖:

@IBAction func childScreenButton(sender: AnyObject) {
    if let delegate = self.delegate {
        delegate.childButtonClickedOnCell(self)
       }
    }

協議:

protocol childTableCellDelegate: class {
func childButtonClickedOnCell(cell: childViewCell)
}

父級ViewController:

func childButtonClickedOnCell(cell: FeedChildViewCell) {
     self.clickedIndexPath = self.feedsTableView.indexPathForCell(cell)
     self.performSegueWithIdentifier("toNextScreen", sender: self)

}

在測試斷點時,未在子視圖上輸入“ delegate.childButtonClickedOnCell(self)”。 請讓我知道這里做錯了什么。 謝謝!!

我懷疑您有幾處地方不對,或者定義不正確。

我對此進行了快速測試,並且委托調用工作正常……看看您是否注意到任何不完全相同的東西……

//
//  TestTableViewController.swift
//
//  Created by DonMag on 4/7/17.
//  Copyright © 2017 DonMag. All rights reserved.
//

import UIKit

protocol MyCellDelegate {
    func pressedButtonForMyCell(theSender: MyCell)
}

class MyCell: UITableViewCell {

    @IBOutlet weak var theLabel: UILabel!
    @IBOutlet weak var theButton: UIButton!

    var delegate: MyCellDelegate?

    @IBAction func childScreenButton(sender: AnyObject) {
        delegate?.pressedButtonForMyCell(theSender: self)
    }

}

class TestTableViewController: UITableViewController, MyCellDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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


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

        cell.theLabel.text = "\(indexPath)"
        cell.delegate = self

        return cell
    }

    func pressedButtonForMyCell(theSender: MyCell) {
        print("delegate called", theSender)
    }

}

暫無
暫無

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

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