2
votes

I'm Trying to learn how to do a detail view for my project .

I have a simple tableView with a simple Array data to fill it.

The Table View :

TableView Example

I designed a detail View as well, with static tableViewCells

Detail View example :

Example

I'v Connected both with a segue :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("Profile", sender: indexPath);

        }

I also connected all the labels and images with Outlets i want to change between each cell but i don't how to advance from here.

right now every cell shows the same thing but i want to change the data between rows . So i would like to change the data through the segue and create a master detail application like in my tableview. Can anybody help me ?

Am using Swift 2.3 and Xcode 8.1

2
I don't understand your problem and desired outcome well enough to help. - Frankie
Im trying to create a master detail application like in my tableView - adirbuilder
You could start by adding a screen shot of what you're seeing and then clarifying which example is your expected result and adding a bit more code around what you've already tried. - Frankie
I didnt do much i just set the segue cause i dont know how to continue or how to send data or change it - adirbuilder
Well, SO is for specific questions. I have no idea what you want to do. So my recommendation is if you're really stuck then google for some master-detail tutorials and walk through them until you get it. - Frankie

2 Answers

1
votes

If I understand your question correctly, you just want to pass dataSource element to the next viewController. So you can just pick it using indexPath.row and use sender parameter to set it in prepareForSegue method.

The code below assumes your dataSource is self.users array.

Swift 3

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let user = self.users[indexPath.row]
    self.performSegueWithIdentifier("Profile", sender: user)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let segueId = segue.identifier? else { return }
    if segueId == "Profile" {
        guard let profileVC = segue.destination as? ProfileViewController else { return }
        profileVC.user = sender as? User
    }
}

Swift 2

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let user = self.users[indexPath.row]
    self.performSegueWithIdentifier("Profile", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    guard let segueId = segue.identifier else { return }
    if segueId == "Profile" {
        guard let profileVC = segue.destinationViewController as? ProfileViewController else { return }
        profileVC.user = sender as? User
    }
}

Edit

im trying to change data like al the labels you saw between rows like for example shalvata will have a different data from light house and so , change the labels and images and so on

It is still unclear for me what data you want to change exactly. Also I don't understand the language on your screenshots, but since you name the relationship as master-detail, I suppose the second screen is meant to show more info about the entity selected on the first screen. If so, you should start from designing you model so that it contains all those fields you need on the second screen. Judging by the icons it would be something like

struct Person {
    var name: String?
    var image: UIImage?
    var age: Int?
    var address: String?
    var phone: String?
    var schedule: String?
    var music: String?
    var smoking: Bool?
    var car: String?
    var info: String?
    var hobby: String?
}

Note: Remove ? for those fields which aren't optionals, i.e. always must be set for every entity (perhaps name field)

Usage

I don't known how and when you create your Person array, but basically there are two approaches:

  1. Use a list of entities with all fields filled on MasterVC and just pass the selected person to the DetailVC in didSelectRowAtIndexPath
  2. Use a list of entities with some basic data (name, address, image) required for MasterVC and fill the rest of the fields only when required (didSelectRowAtIndexPath method)

In any case you'll get selected person in DetailVC and now everything you need is to use that data in cellForRow method, just as you did on MasterVC. Perhaps it would be a better option to use static TableViewController for Details screen.

0
votes

Sounds like what you're trying to do does not involve segues at all. You can change data of cells using the cellForRow method in your tableViewController.

https://developer.apple.com/reference/uikit/uitableview/1614983-cellforrow

For example

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "foo"
    return cell
}

If that sounds confusing to you then you should take a step back and do some tutorials then post specific questions on SO.