2016-07-26 37 views

回答

2

UITableView共享一个单元格的唯一方法是将它们定义在一个.xib文件中,并为它们提供一个重用标识符。然后在代码中将该文件/相应的类注册到UITableView

例如把这个你tableViewController的viewDidLoad()内:

tableView.registerNib(UINib(nibName: "", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "") 

然后你就可以继续正常里面cellForRowAtIndexPath

+0

谢谢,请注意,在'UINib'初始化程序 –

+0

@appzYourLife中有一个缺少的参数,您是正确的,谢谢。从没有Xcode的内存中键入,忘记了它。 –

1

你必须创建一个笔尖文件(的.xib),你可以做,通过做文件 - > - >查看(的UserInterface第)

enter image description here

然后设置你的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 
} 
相关问题