如何检测nib文件表视图单元格使ReusableCell?

我需要添加单元格标识符来为tableView创buildReusableCell。 但是我没有看到任何celltableView 属性表视图分层 。 如何在表格视图中添加单元格。

注意: 基本上我想创build一个应该包含一个tableView和该tableView应该有自定义的UiTableViewCell

在这里输入图像说明

代码在这里:

 class SuggestNearTableViewCollectionViewCell: UICollectionViewCell , UITableViewDataSource,UITableViewDelegate{ @IBOutlet weak var suggestTableView : UITableView! override func awakeFromNib() { super.awakeFromNib() // Initialization code self.suggestTableView.dataSource = self self.suggestTableView.delegate = self suggestTableView.register(UINib(nibName: "SuggestNearTableViewCell", bundle: nil), forCellReuseIdentifier: "SuggestNearTableViewCell") } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "SuggestNearTableViewCell", for: indexPath) as! SuggestNearTableViewCell return cell } } 

首先进入File-> new – > Cocoa Touch Class,创buildUIViewCOntroller类。 相应地命名你的class级。

在这里输入图像说明

现在你将有一个Xib文件和一个Swift文件。 Xib看起来像这样。

在这里输入图像说明

现在在Xib上拖放一个UITableView,并给它4引脚的约束,如top = 0,bottom = 0,leadin = 0,trailing = 0。 现在在你新创build的swift文件中创build你的tableView的出口。 连接数据源和委托。

现在再次进入File-> New-> Coucoa Touch Class,并为UItableViewCell创build一个类,并创build一个如下所示的Xib文件。

在这里输入图像说明

现在你将有一个Xib为你的单元格和一个快速的文件为您的单元格。 只要在你的Xib中devise你的手机就可以了。 比方说,如果你想放一个imageView或一个标签等,然后创build您的自定义单元格的swift文件中的所有组件的出口。 现在在你的自定义单元格的swift文件中添加这个函数。

  class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell { let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier" tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier) let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell return cell } 

您的自定义单元已准备好使用。

现在转到您的tableView的swift文件,并在您的cellForRowAtIndexPath只使用像这样使用下面的单元格。

 let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath) cell.myImageView.image = "something" // Do something with your cell 

我希望这将是有益的。 如果您发现任何困难,请告诉我。

viewDidLoad tableViewController里面,你应该像这样注册它:

tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")

cellForRowAtIndexPath里面只声明单元格:

 let cell: CustomOneCell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell 

如果你不在tableViewController里面,那么你必须连接你的tableView和delegate:

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! ... override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // Table view delegate self.tableView.delegate = self self.tableView.dataSource = self ...