在 tableViewCell 中使用 segue 传递数据

Passing data with segue in the tableViewCell

我想在 tableViewCell 中使用 segue 传递数据,从 BulletinBoadrViewController 到 BbDetailViewController

class BulletinBoadrViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!


var bulletinBoards = [BulletinBoard]()


override func viewDidLoad() {
    super.viewDidLoad()

    bulletinBoards = BulletinBoard.downloadAllBulletinBoard()

    self.tableView.reloadData()


    tableView.estimatedRowHeight = tableView.rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.separatorStyle = .none

}

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

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


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BulletinBoardTableViewCell
    let bulletinBoard = bulletinBoards[indexPath.row]
    cell.bulletinBoard = bulletinBoard
    return cell

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "gotodetail", sender: indexPath)
    print("Row \(indexPath.row)selected")

}




func prepareForSegue(segue: UIStoryboardSegue, sender: Any!) {
    if segue.identifier == "gotodetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow  {
            let destVC = segue.destination as! BdDeatilViewController
            let new = bulletinBoards[indexPath.row]
            destVC.bulletinBoard = new


        }
    }

}

它是 BdDeailViewController

class BdDeatilViewController:UIViewController {


@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var contentLabel: UITextView!



@IBAction func backtobb(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)

}

var x = [BulletinBoard]()

var bulletinBoard : BulletinBoard!{
    didSet{
        self.updateUI()
    }
}

func updateUI() {
    timeLabel.text = bulletinBoard.time
    titleLabel.text = bulletinBoard.title
    contentLabel.text = bulletinBoard.content
}

override func viewDidLoad() {
    super.viewDidLoad()


}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

}

} tableViewCell 的数据取自本地 json 文件,它是 BulletinBoard code

class BulletinBoard {

var title:String?
var time:String?
var content:String?

init(title:String,time:String,content:String) {
    self.title = title
    self.time = time
    self.content = content
}

init(bulletinBoardDictionary:[String:Any]) {
    self.title = bulletinBoardDictionary["title"] as? String
    self.time = bulletinBoardDictionary["time"] as? String
    self.content = bulletinBoardDictionary["content"] as? String
}

static func downloadAllBulletinBoard() -> [BulletinBoard] {

    var bulletinBoards = [BulletinBoard]()

    //get the json data from the file

    let jsonFile = Bundle.main.path(forResource: "BulletinBoardData", ofType: "json")
    let jsonFileURL = URL(fileURLWithPath: jsonFile!)
    let jsonData = try? Data(contentsOf: jsonFileURL)


    //turn the json data into foundation objects (bulletinBoards)

    if let jsonDictionary = NetworkService.parseJSONFromData(jsonData) {
        let bulletinBoardDictionaries = jsonDictionary["BulletinBoard"] as! [[String:Any]]

        for bulletinBoardDictionary in bulletinBoardDictionaries {
            let newBulletinBoard = BulletinBoard(bulletinBoardDictionary: bulletinBoardDictionary)
            bulletinBoards.append(newBulletinBoard)
        }

    }

    return bulletinBoards
}

}

最后,这是我的 StoryBoard

https://i.stack.imgur.com/JcgdH.png1

谁能解决我的问题?谢谢!

我认为你应该在 prepareForSegue 中从 sender 中检索 indexPath:

override func prepareForSegue(segue: UIStoryboardSegue, sender: Any!) {
    if segue.identifier == "gotodetail" {
        if let indexPath = sender as? IndexPath {
            let destVC = segue.destination as! BdDeatilViewController
            let new = bulletinBoards[indexPath.row]
            destVC.bulletinBoard = new
        }
    }
}

并更新 viewDidLoad 中的 UI:

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