2016-07-26 64 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 
} 
相關問題