Reputation: 343
I'm creating a couple of cards with information to display. Each card is going to be represented with a UIView
, so I'm creating each card in different .xib files.
One of my cards contains a custom UITableView
and cells which are going to be loaded and managed with the UITableViewDataSource
and UITableViewDelegate
protocols.
However when I search for the Table View component and drag it to my .xib file it loads a predefined UITableView which I can't customize. I'm fairly new to using separate .xib files (I've been using storyboards) so I don't know how to customize my TableView in this context so any help would be appreciated.
Upvotes: 0
Views: 195
Reputation: 548
make a tableview and create a tableview instance. write this code in viewdidload... self.tableView.registerNib(UINib(nibName: "PackageDetailTVCell", bundle: nil), forCellReuseIdentifier: "Cell")
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return self.arrayDict.count }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PackageDetailTVCell
let dict = self.arrayDict[indexPath.row]
cell.labelOfferPrice.text = (dict .objectForKey("offer_price") as! String)
cell.labelName.text = (dict .objectForKey("title") as! String)
cell.labelRegularPrice.text = (dict .objectForKey("price") as! String)
return cell
}
Upvotes: 0
Reputation: 11597
Xibs dont have the tableviewcell prototypes like in the storyboard, unfortunately its a storyboard only feature. Xibs are a bit archaic but still have their purpose. But you can have the different cells within the same xib file as your tableview, they will just be separate to the tableview.
you would load the xib like
var objects: NSArray?;
NSBundle.mainBundle().loadNibNamed("MyTableView", owner: self, topLevelObjects: &objects)
then in the array, depending on the order of the elements in the xib, you will get back an array of all the elements in your xib. so probably at position 0 will be your tableview, then 1 will be a cell, 2 a cell etc.. depending how many you have. you'll probably only load the cells in cellForRowAtIndexPath:
only though, while the table will be in viewDidLoad
or something
Upvotes: 1