Passing data with segue in the tableViewCell

廖豪豪

I want to passing data with segue in the tableViewCell,from BulletinBoadrViewController to 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


        }
    }

}

and it's 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)

}

} and tableViewCell's data is taking from local json file, it's 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
}

}

Finally,it's my StoryBoard

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

Can anyone solve my problem?Thanks!

Danh Huynh

I think you should retrieve indexPath from sender in prepareForSegue:

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
        }
    }
}

and update the UI in the viewDidLoad:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass tableviewcell data with segue?

From

Passing data with unwind segue

From Dev

Passing data with segue

From Dev

Passing data through Segue

From Dev

Passing data from TableViewController to TableViewCell

From Dev

Passing data with segue through TabBarControllerController

From Dev

Passing data with segue through navigationController

From Dev

Swift: Prepare for segue not passing data

From Dev

Passing data between storyboards with no segue

From Dev

Passing Firebase data from TableViewCell to another ViewController

From Dev

Passing Data from TableViewCell labels to a ViewController

From Dev

Passing data from tableViewCell to another VC

From Dev

Passing data between View Controllers using Segue

From Dev

Passing Data between View Controllers without segue

From Dev

Swift one out of three segue is not passing data

From Dev

Passing data with segue from UITableView to UIViewController

From Dev

Passing data from modal segue to parent

From Dev

Passing custom data with map pin segue

From Dev

Passing data on segue to view controller (Swift)

From Dev

How to get json data for passing it by segue

From Dev

Passing data to new ViewController through Segue

From Dev

iOS Delegates instead of passing data through a segue

From Dev

Passing Data with without Segue from Container View to MainVC

From Dev

issue passing data to segue.destination within function

From Dev

passing tapped cell data to another view via segue in Swift

From Dev

Passing Parsed Data from the Parent view to Child view using segue

From Dev

Passing data in segue from plist. Table view to Details View

From Dev

How to pass data from TableViewCell class of a View Controller to another ViewController via segue Swift 4?

From Dev

Passing UIImage with segue

Related Related

  1. 1

    Pass tableviewcell data with segue?

  2. 2

    Passing data with unwind segue

  3. 3

    Passing data with segue

  4. 4

    Passing data through Segue

  5. 5

    Passing data from TableViewController to TableViewCell

  6. 6

    Passing data with segue through TabBarControllerController

  7. 7

    Passing data with segue through navigationController

  8. 8

    Swift: Prepare for segue not passing data

  9. 9

    Passing data between storyboards with no segue

  10. 10

    Passing Firebase data from TableViewCell to another ViewController

  11. 11

    Passing Data from TableViewCell labels to a ViewController

  12. 12

    Passing data from tableViewCell to another VC

  13. 13

    Passing data between View Controllers using Segue

  14. 14

    Passing Data between View Controllers without segue

  15. 15

    Swift one out of three segue is not passing data

  16. 16

    Passing data with segue from UITableView to UIViewController

  17. 17

    Passing data from modal segue to parent

  18. 18

    Passing custom data with map pin segue

  19. 19

    Passing data on segue to view controller (Swift)

  20. 20

    How to get json data for passing it by segue

  21. 21

    Passing data to new ViewController through Segue

  22. 22

    iOS Delegates instead of passing data through a segue

  23. 23

    Passing Data with without Segue from Container View to MainVC

  24. 24

    issue passing data to segue.destination within function

  25. 25

    passing tapped cell data to another view via segue in Swift

  26. 26

    Passing Parsed Data from the Parent view to Child view using segue

  27. 27

    Passing data in segue from plist. Table view to Details View

  28. 28

    How to pass data from TableViewCell class of a View Controller to another ViewController via segue Swift 4?

  29. 29

    Passing UIImage with segue

HotTag

Archive