Reputation: 463
Im trying to create a Master detail View Application & I'm kinda stuck.
I have a tableView with 100 DynamicPrototype
Cells
showing Bars and when i tap on a cell
i want to present more info about that Bar.
My question is how to fill different data
like labels
and images
into the DetailView
and change them for each cell
i tap to represent the correct data
My Detailview
is a TableViewController
with StaticCells
as shown here:
I am using Swift 2.3 and Xcode 8.1
I created a class to represent my DetailView Data :
import Foundation
class BarProfile {
var HeaderImage = ""
var HeaderTitle = ""
var Age = ""
var Adress = ""
var Number = ""
var Time = ""
var Music = ""
var Info = ""
var Menu = ""
var More = ""
init(HeaderImage: String, HeaderTitle: String, Age: String, Adress: String, Number: String, Time: String, Music: String, Info: String, Menu: String, More: String) {
self.HeaderImage = HeaderImage
self.HeaderTitle = HeaderTitle
self.Age = Age
self.Adress = Adress
self.Number = Number
self.Time = Time
self.Music = Music
self.Info = Info
self.Menu = Menu
self.More = More
}
}
My BarsTableView
that looks like this :
import UIKit
class BarsTableViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate{
@IBOutlet var tableViewController: UITableView!
// MARK : Data
var names = ["Shalvata",
"Markid",
"Litzman Bar",
"The Cat & The Dog",
"Light house",
var streets =
["האנגר 28,נמל תל אביב",
"אבן גבירול 30,תל אביב",
"רחוב נמל תל אביב",
"קרליבך 28,תל אביב",
"האנגר 23,נמל תל אביב"]
var images = [UIImage(named: "Shalvata"),
UIImage(named: "Markid"),
UIImage(named: "Litzman Bar"),
UIImage(named: "CatNDog"),
UIImage(named: "LightHouse")]
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 100.5;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
////////
////////
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
let 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, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("BarsProfile", sender: nil)
}
}
I didn't find a tutorial that shows what I'm really looking for and i was hoping to find the answer here cause detailView is the basic of every project or App .
I don't know how to continue and I'm pretty frustrated about it.
Thanks for helping :)
Upvotes: 1
Views: 40
Reputation: 530
You can pass the data to the detail view through the prepareForSegue
function and set variables or call functions in the destination view.
Here's an example of how it could be done. This assumes that the view you use to present 'detail' is of type DetailView
and the segue from your master -> Detail is called 'BarsProfile':
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "BarsProfile") {
if(segue.destination is DetailView) {
let destVC = segue.destination as! DetailView
// Set variables in Detail View here
// e.g. :
destVC.detailData = selectedData
}
}
}
Upvotes: 1