Search code examples
iosswiftuitableviewmaster-detail

How to make DetailView change data by indexPath


I'm currently working on a Master-DetailView application and I'm stuck on how to make the data change....

I saw a great tutorial on how to do this :

Tutorial

But the guy is using a blank ViewController & I'm using a TableViewController With static Cells,So it doesn't work.

I want to put the data manually like

var Label1Data = ["You Tapped Cell 1,You Tapped Cell 2,You Tapped Cell 3"]

and it will show in the DetailView by the index path if i pressed the first cell the first data will show up in that Label...i know its not ideal to use static cells here but i do wanna use them design wise.

It will be great if any one could show me finally how can i put the data successfully like i said above and how the Tutorial does it.

MasterViewController Code:

import UIKit
import AVFoundation


class BarsViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate,UITabBarControllerDelegate{

    @IBOutlet var tableViewController: UITableView!
    var audioPlayer = AVAudioPlayer()
    var sel_val : String?

    // TableView Data :

struct User {
        var name: String
        var streetName: String
        var image: UIImage?
    }

var allUsers: [User]!
    var filteredUsers: [User]!

    func createUsers(names: [String], streets: [String], images: [UIImage?]) -> [User] {
        var users = [User]()
        guard names.count == streets.count && names.count == images.count else { return users }
        for (index, name) in names.enumerated() {
            let user = User(name: name, streetName: streets[index], image: images[index])
            users.append(user)
        }
        return users
    }

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

        if tableView == self.tableView {

        return self.names.count

        } else {
            return self.filteredUsers.count
        }

    }



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

        let user:User!

        if tableView == self.tableView {
            user = allUsers[indexPath.row]
        } else {
            user = filteredUsers[indexPath.row]
        }
        cell.photo.image = user.image
        cell.name.text = user.name
        cell.streetName.text = user.streetName

        return cell
    }

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


        let object = self.storyboard?.instantiateViewController(withIdentifier: "BarProfileTableViewController") as! BarsProfile
        let user:User!
        if tableView == self.tableView {
            user = allUsers[indexPath.row]
        } else {
            user = filteredUsers[indexPath.row]
        }

      print("username : \(user.name)")
        print("streetName : \(user.streetName)")

        MyIndex = indexPath.row

        object.barImage = user.image!
        object.barName = user.name
        object.streetName = user.streetName



        self.navigationController?.pushViewController(object, animated: true)

    }

DetailView's Code:

import UIKit
import AVFoundation
import MapKit

class BarsProfile: UITableViewController,MKMapViewDelegate {


    @IBOutlet var Distance: UILabel!
    @IBOutlet var headerImage: UIImageView!
    @IBOutlet var OnlineMenu: UIButton!
    @IBOutlet var Address: UILabel!
    @IBOutlet var ProfileMapView: MKMapView!
    @IBOutlet var BarNameLBL: UILabel!
    @IBOutlet var streetNameLBL: UILabel!
    @IBOutlet var MusicLabel: UILabel!
    @IBOutlet var KindOfBarCell: UITableViewCell!



    var barName = String()
    var barImage = UIImage()
    var streetName = String()


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

        BarNameLBL.text = barName
        streetNameLBL.text = streetName
        navigationItem.title = barName


    }

How it looks like : ( Red line is a label i would like to put data manually in)

enter image description here


Solution

  • you cant use a TableViewController for your purpose. Instead you can use a normal ViewController with labels and textfields.

    enter image description here

    In the above picture of yours, you can use a simple label of width = 1 and color = lightGrey so that you can get the same separator line as in the tableview.