与多个UITableView相同的原型单元格
问题描述:
在Storyboard中,我可以定义一个(或多个)原型单元格以与UITableView
一起使用。与多个UITableView相同的原型单元格
但是我需要使用与多个UITableView(s)相同的原型单元格?
可能吗? 谢谢。
答
跨UITableView
共享一个单元格的唯一方法是将它们定义在一个.xib文件中,并为它们提供一个重用标识符。然后在代码中将该文件/相应的类注册到UITableView
。
例如把这个你tableViewController的viewDidLoad()
内:
tableView.registerNib(UINib(nibName: "", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "")
然后你就可以继续正常里面cellForRowAtIndexPath
答
你必须创建一个笔尖文件(的.xib),你可以做,通过做文件 - >新 - >查看(的UserInterface第)
然后设置你的UITableViewCell元素并设置一些reuseIdentifier,你还可以创建子类,如果你需要在你的viewController(您想使用此自定义tableViewCell)
在viewDidLoad中
,那么你需要注册笔尖与类似的tableView,
override func viewDidLoad() {
// the .xib filename
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCellId")
}
那么的cellForRowAtIndexPath,你可以正常使用,如原型细胞,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellId", forIndexPath: indexPath) as! Customcell
// your custom code
return cell
}
谢谢,请注意,在'UINib'初始化程序 –
@appzYourLife中有一个缺少的参数,您是正确的,谢谢。从没有Xcode的内存中键入,忘记了它。 –