在Swift中从nib自定义UITableViewCell

我试图从一个笔尖创build一个自定义表格视图单元格。 我在这里指的是这篇文章。 我面临两个问题。

我创build了一个带有UITableViewCell对象的.xib文件。 我创build了UITableViewCell一个子类,并将其设置为单元的类和Cell作为可重用的标识符。

 import UIKit class CustomOneCell: UITableViewCell { @IBOutlet weak var middleLabel: UILabel! @IBOutlet weak var leftLabel: UILabel! @IBOutlet weak var rightLabel: UILabel! required init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder) } override init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } 

在UITableViewController中我有这个代码,

 import UIKit class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate { var items = ["Item 1", "Item2", "Item3", "Item4"] override func viewDidLoad() { super.viewDidLoad() } // MARK: - UITableViewDataSource override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return items.count } override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let identifier = "Cell" var cell: CustomOneCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell if cell == nil { tableView.registerNib(UINib(nibName: "CustomCellOne", bundle: nil), forCellReuseIdentifier: identifier) cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell } return cell } } 

此代码没有错误,但是当我在模拟器中运行它时,它看起来像这样。

在这里输入图像说明

在故事板的UITableViewController中,我没有对单元格做任何事情。 空白标识符和没有子类。 我尝试添加单元格标识符到原型单元格,并再次运行,但我得到相同的结果。

我面对的另一个错误是,当我试图在UITableViewController中实现以下方法。

 override func tableView(tableView: UITableView!, willDisplayCell cell: CustomOneCell!, forRowAtIndexPath indexPath: NSIndexPath!) { cell.middleLabel.text = items[indexPath.row] cell.leftLabel.text = items[indexPath.row] cell.rightLabel.text = items[indexPath.row] } 

正如我提到的文章所示,我将cell参数的typesformsUITableViewCell CustomOneCell为我的UITableViewCell的子类CustomOneCell 。 但是我得到以下错误,

使用select器'tableView:willDisplayCell:forRowAtIndexPath:'具有不兼容的types'(UITableView !, CustomOneCell !, NSIndexPath!) – >()'的覆盖方法

任何人有任何想法如何解决这些错误? 这些似乎在Objective-C中工作正常。

谢谢。

编辑:我只是注意到,如果我改变模拟器的方向风景,并把它回到肖像,单元格出现! 我仍然无法弄清楚发生了什么事。 我在这里上传了一个Xcode项目,如果你有时间快速查看,那么这个项目就会显示出问题。

你应该为你的项目尝试下面的代码(更新Swift 2):

CustomOneCell.swift

 import UIKit class CustomOneCell: UITableViewCell { // Link those IBOutlets with the UILabels in your .XIB file @IBOutlet weak var middleLabel: UILabel! @IBOutlet weak var leftLabel: UILabel! @IBOutlet weak var rightLabel: UILabel! } 

TableViewController.swift

 import UIKit class TableViewController: UITableViewController { let items = ["Item 1", "Item2", "Item3", "Item4"] override func viewDidLoad() { super.viewDidLoad() tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne") } // MARK: - UITableViewDataSource override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell cell.middleLabel.text = items[indexPath.row] cell.leftLabel.text = items[indexPath.row] cell.rightLabel.text = items[indexPath.row] return cell } } 

下面的图片显示了一组约束条件,它们与所提供的代码一起工作,没有来自Xcode的任何约束模糊消息。

在这里输入图像说明

这是我使用Swift 2和Xcode 7.3的方法。 这个例子将使用一个ViewController加载两个.xib文件 – 一个用于UITableView,一个用于UITableCellView。

在这里输入图像说明

对于这个例子,你可以把一个UITableView放到一个空的TableNib .xib文件中。 在里面,将文件的所有者设置为您的ViewController类,并使用sockets来引用tableView。

在这里输入图像说明

在这里输入图像说明

现在,在你的视图控制器中,你可以象往常一样委托tableView

 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 ... 

要再次创build自定义单元格,请将Table View Cell对象放入一个空的TableCellNib .xib文件中。 这一次,在单元格.xib文件中,您不必指定“所有者”,但您需要指定一个自定义类和一个标识符,如“TableCellId”

在这里输入图像说明 在这里输入图像说明

用你需要的任何sockets来创build你的子类

 class TableCell: UITableViewCell { @IBOutlet weak var nameLabel: UILabel! } 

最后…回到你的视图控制器,你可以像这样加载和显示整个东西

 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // First load table nib let bundle = NSBundle(forClass: self.dynamicType) let tableNib = UINib(nibName: "TableNib", bundle: bundle) let tableNibView = tableNib.instantiateWithOwner(self, options: nil)[0] as! UIView // Then delegate the TableView self.tableView.delegate = self self.tableView.dataSource = self // Set resizable table bounds self.tableView.frame = self.view.bounds self.tableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // Register table cell class from nib let cellNib = UINib(nibName: "TableCellNib", bundle: bundle) self.tableView.registerNib(cellNib, forCellReuseIdentifier: self.tableCellId) // Display table with custom cells self.view.addSubview(tableNibView) } 

代码显示了如何加载和显示一个nib文件(表格),以及如何注册一个用于单元格使用的nib。

希望这可以帮助!!!

为了修复“重写方法…有不兼容的types…”错误我已经改变了函数声明

 override func tableView(tableView: (UITableView!), cellForRowAtIndexPath indexPath: (NSIndexPath!)) -> UITableViewCell {...} 

(是-> UITableViewCell! – 在最后带有感叹号)

另一种方法,可能为你工作(这是我怎么做)注册一个类。

假设你创build一个像下面这样的自定义tableView:

 class UICustomTableViewCell: UITableViewCell {...} 

然后你可以注册这个单元格的任何UITableViewController你将显示它与“registerClass”:

 override func viewDidLoad() { super.viewDidLoad() tableView.registerClass(UICustomTableViewCell.self, forCellReuseIdentifier: "UICustomTableViewCellIdentifier") } 

你可以像在单元格中对行方法所期望的那样调用它:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("UICustomTableViewCellIdentifier", forIndexPath: indexPath) as! UICustomTableViewCell return cell }