如何使用Swift在UITableview中加载自定义单元格(Xib)

这是我的代码

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell! bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered") //NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell cell = blucell devname.text = peri[indexPath.row] devstrength.text = signalstrength[indexPath.row] bluetoothlog.backgroundColor = UIColor.clearColor() return cell } 

我已经尝试了上面的代码,没有什么显示在Tableview中,请帮我改变这个代码的工作

谢谢

 import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { //MARK:- Sample Data Array to load in TableView let sampleArrray: \[String\] = \["val1", "val2", "val3", "val4", "val5"] //MARK:- Cell reuse identifier let cellReuseIdentifier = "cell" //MARK:- UITableView outlet @IBOutlet var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Registering the custom cell self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) // Setting delegate and Datasourse as same viewcontroller (this code is not neccessory if we are setting it from storyboard) tableView.delegate = self tableView.dataSource = self } //UITableview required methods func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sampleArrray.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell! cell.textLabel?.text = sampleArrray\[indexPath.row\] return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("clicked at: \(indexPath.row)") //Here u can get the selected cell and add action related to this cell seelction } 

添加了屏幕截图,用于设置故事板中的数据源和委托 在这里输入图像说明

  1. 用重用标识符注册您的NIB:

     override func viewDidLoad() { super.viewDidLoad() tableView.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "Cell") } 
  2. 您的cellForRowAtIndexPath将然后实例化单元格。

     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! devicedet return cell } 
  • 应该在ViewDidLoad中调用registerNib命令,而不是在每行的单元调用中调用
  • 什么是'bluecell'? 在你的callbackcellforTable ..,你分配cell = blucell – >这可能会导致你的问题。

尝试这个:

 import UIKit class YourViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate { override func viewDidLoad() { super.viewDidLoad() //call register nib here!! bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return peri.count } //it seem that you have to add a è func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! { let cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell! /* REMOVE THIS bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered") //NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell */ //// THEN, UPDATE YOUR CELL with YOUR DATAs /* I don't understand what are you doing with this line of code: cell = blucell */ devname.text = peri[indexPath.row] devstrength.text = signalstrength[indexPath.row] //Seem that bluetoothlog is the tableview, you should call it in viewdidload or viewwillappear() bluetoothlog.backgroundColor = UIColor.clearColor() return cell } } 
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell :devicedet = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell! bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered") //NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell cell = blucell devname.text = peri[indexPath.row] devstrength.text = signalstrength[indexPath.row] bluetoothlog.backgroundColor = UIColor.clearColor() return cell } 

在viewDidLoad中注册XIB如下图所示

 var userDetailsNIB = UINib(nibName: "UserDetailsCell", bundle: nil) viewListingTable.registerNib(userDetailsNIB, forCellReuseIdentifier: "UserDetailsViewCellID") And in func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ function use it by access it with Identifier which created in viewDidLoad method let cell = tableView.dequeueReusableCellWithIdentifier("UserDetailsViewCellID") as UserDetailsViewCell return cell 

如果您想在UITableView中以swift方式加载自定义单元格(Xib),则可以使用以下代码。

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count; //This function returns number of rows in table view } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell:YourTableViewCell! = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell", forIndexPath:indexPath)as! YourTableViewCell // Do additional code here } 

不要忘记在viewDidLoad中注册nib

 override func viewDidLoad() { super.viewDidLoad() // registering your nib let yourNibName = UINib(nibName: "YourTableViewCell", bundle: nil) tableView.registerNib(yourNibName, forCellReuseIdentifier: "YourTableViewCell") // Do any additional setup after loading the view. } 

在Swift中注册XIB Cell

覆盖函数viewDidLoad(){super.viewDidLoad()tableView.register(UINib(nibName:“XibCell”,bundle:nil),forCellReuseIdentifier:“Cell”)}

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) – > UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier:“Cell”)as! UITableViewCell返回单元格}

我的代码:

 override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "XibCell", bundle: nil), forCellReuseIdentifier: "Cell") } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell return cell } 

你的functionregisterNib,你尝试在viewDidLoad()方法中写入,并从cellForRowAtIndexPah中删除,然后testing你的项目

您的单元实例应该是CustomCell实例,而不是UITableView单元。 var cell:UITableViewCell应该被var cell:devicedetreplace