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
}
}
Thanks for helping :)
Upvotes: 0
Views: 146
Reputation: 2505
Its very simple you have just consider you are showing headerTitle
in viewController1
and if your select any row in detailTableViewController
you are gonna to show age
, address
and number
(You can show want ever you want by creating extra static cells and placing the label in it.)
Your modified class:
First declare all properties as optional and optional in init() method also
import Foundation
class BarProfile {
var HeaderImage: String?
var HeaderTitle: String?
var Age: String?
var Adress: String?
var Number: String?
var Time: String?
var Music: String?
var Info: String?
var Menu: String?
var More: String?
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
}
}
Populate the ViewController1
table view as usual and when the user taps any cell pass that selected object to detailTableViewController
. Create the segue from your table view cell in ViewController1
to detailTableViewController
.
Code in ViewController1:
class viewController1: UIViewController, UITableViewDataSource, UITableViewDelegate {
var barArray:[BarProfile] = []
var selctedIndexPath: NSIndexPath?
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...5 { // Creating dummy object for clear understanding.
let barObject = BarProfile(HeaderImage: "werw", HeaderTitle: "erqwrw", Age: "34", Adress: "qwerwer", Number: "32", Time: "time", Music: "qerqw", Info: nil, Menu: nil, More: nil)
barArray.append(barObject)
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return barArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell Identifier")! as UITableViewCell
cell.textLabel?.text = barArray[indexPath.row].HeaderTitle
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selctedIndexPath = indexPath
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: Navigation Methods
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueIdentifier" {
let detailViewController = segue.destinationViewController as? detailTableViewController
detailViewController?.selectedBarObject = barArray[(selctedIndexPath?.row)!]
}
}
}
Code in your detailTableViewController:
class detailTableViewController: UITableViewController {
var selectedBarObject: BarProfile?
@IBOutlet var ageLabel: UILabel! // connect these outlets to your label in static cells.
@IBOutlet var addressLabel: UILabel!
@IBOutlet var numberLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if selectedBarObject != nil {
showBarDetails()
} else {
// Do nothing
}
}
// You are going to use static cells so no need to implement table view data source methods
func showBarDetails() {
ageLabel.text = selectedBarObject?.Age
addressLabel.text = selectedBarObject?.Adress
numberLabel.text = selectedBarObject?.Number
}
}
Thanks:)
Upvotes: 1
Reputation: 80265
Shalom, it's actually quite simple:
It seems your detail view is also a table view. So implement that the same way as the other table view.
Return your 8 cells, and create different cell prototypes (from the above screenshot, it seems you need three or so). In cellForRowAtIndexPath
you check which row you are in and populate the cell with the correct image and the correct data field from your BarProfile
object (which should be an ivar of the view controller).
The image and title can be implemented as a tableHeaderView
.
Upvotes: 0