简体   繁体   English

如何在.xib文件中使用自定义UITableView和UITableViewCell?

[英]How to use a custom UITableView and UITableViewCell inside a .xib file?

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. 每张卡都将使用UIView表示,因此我将在不同的.xib文件中创建每张卡。 One of my cards contains a custom UITableView and cells which are going to be loaded and managed with the UITableViewDataSource and UITableViewDelegate protocols. 我的一张卡包含一个自定义UITableView和将要使用UITableViewDataSourceUITableViewDelegate协议加载和管理的单元。

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. 但是,当我搜索Table View组件并将其拖到我的.xib文件中时,它将加载一个无法自定义的预定义UITableView。 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. 我对使用单独的.xib文件是相当陌生的(我一直在使用情节提要),所以我不知道如何在这种情况下自定义TableView,因此将不胜感激。

Xibs dont have the tableviewcell prototypes like in the storyboard, unfortunately its a storyboard only feature. Xibs没有像情节提要中那样具有tableviewcell原型,不幸的是,它仅是情节提要中的功能。 Xibs are a bit archaic but still have their purpose. Xibs有点陈旧,但仍有其用途。 But you can have the different cells within the same xib file as your tableview, they will just be separate to the tableview. 但是您可以在与表视图相同的xib文件中包含不同的单元格,它们将仅与表视图分开。

you would load the xib like 你会像这样加载XIB

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. 然后在数组中,根据xib中元素的顺序,您将获得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. 因此,位置0可能是您的表格视图,然后1将是一个单元格,2将是一个单元格,依此类推。。 you'll probably only load the cells in cellForRowAtIndexPath: only though, while the table will be in viewDidLoad or something 您可能只会在cellForRowAtIndexPath:加载单元格cellForRowAtIndexPath:尽管如此,但表将位于viewDidLoad或其他内容中

make a tableview and create a tableview instance. 创建一个tableview并创建一个tableview实例。 write this code in viewdidload... self.tableView.registerNib(UINib(nibName: "PackageDetailTVCell", bundle: nil), forCellReuseIdentifier: "Cell") 在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,numberOfRowsInSection部分: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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM