【发布时间】:2016-07-26 11:57:59
【问题描述】:
在 Storyboard 中,我可以定义一个(或多个)原型单元以与 UITableView 一起使用。
但是我需要将同一个原型单元与多个 UITableView(s) 一起使用?
有可能吗? 谢谢。
【问题讨论】:
标签: ios swift uitableview uikit
在 Storyboard 中,我可以定义一个(或多个)原型单元以与 UITableView 一起使用。
但是我需要将同一个原型单元与多个 UITableView(s) 一起使用?
有可能吗? 谢谢。
【问题讨论】:
标签: ios swift uitableview uikit
在UITableView 之间共享单元格的唯一方法是在 .xib 中将它们定义为单独的文件,并为它们提供重用标识符。然后在代码中将该文件/相应的类注册到UITableView。
例如将其放在 tableViewController 的 viewDidLoad() 中:
tableView.registerNib(UINib(nibName: "", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "")
然后您就可以在cellForRowAtIndexPath 中照常继续了
【讨论】:
UINib初始化器中缺少参数
你必须创建一个 nib 文件 (.xib),你可以通过 File -> New -> View (在用户界面部分下)
然后设置你的 UITableViewCell 元素并设置一些reuseIdentifier,你也可以根据需要创建子类
然后在您的 viewController 中(您想在其中使用此自定义 tableViewCell)
在 viewDidLoad 中,您需要使用 tableView 注册该笔尖,例如,
override func viewDidLoad() {
// the .xib filename
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCellId")
}
然后在cellForRowAtIndexPath中,就可以像原型cell一样正常使用了,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellId", forIndexPath: indexPath) as! Customcell
// your custom code
return cell
}
【讨论】: