在Swift中以编程方式创建UITableViewCell

我正在尝试为我的UITableView创建一个自定义单元格,但我遇到了一些困难。

首先我不能使用Interface Builder,因为我在Xcode中遇到了这个bug的变种 。 每次我在Interface Builder中单击一个元素时,该视图中的所有内容都会获得零高度和宽度,并在视图外部重新定位。 此外,我想学习如何以编程方式执行此操作。

其次,我正在为我的项目使用Swift语言。 我一直在努力跟随这个演示 ,并尽力将Objective C代码转换为Swift,但每当我遇到问题时,我最终都会陷入困境。 我认为这是因为我没有正确地转换代码。

第三, 我发现了这个video但是尽管很难遵循(很多代码只是复制和粘贴而没有太多解释它的作用或原因),它仍然最终使用Interface Builder来改变各个部分。

我有一个基本的UITableView设置很好。 我只是希望能够在该表视图中添加自定义单元格。

  • 这可以使用纯编程来完成,还是需要使用Interface Builder?

  • 任何人都可以指出我正确的方向或帮助我在Swift中以编程方式创建自定义单元格?

非常感谢。

一般来说:在纯编程中一切皆有可能;-)

  1. 为tableView单元格创建一个自定义类,并设置所有元素,属性和可视布局。 实现所需的方法init(style,reuseidentifier)

  2. UITableViewController的自定义类中,使用registerClass(forCellReuseIdentifier)注册自定义单元类

  3. 为自定义tableViewController设置委托和数据源

最后,在cellForRowAtIndexPath创建单元格:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("myReuseIdentifier", forIndexPath: indexPath) as MyCustomTableViewCell // configure the cell using its properties return cell } 

这应该是基本步骤。

如果您正在寻找更多代码,以下是我创建的自定义单元格的示例:

 // File: vDataEntryCell.swift import UIKit class vDataEntryCell: UITableViewCell { //----------------- // MARK: PROPERTIES //----------------- //Locals var textField : UITextField = UITextField() //----------------- // MARK: VIEW FUNCTIONS //----------------- ///------------ //Method: Init with Style //Purpose: //Notes: This will NOT get called unless you call "registerClass, forCellReuseIdentifier" on your tableview ///------------ override init(style: UITableViewCellStyle, reuseIdentifier: String!) { //First Call Super super.init(style: style, reuseIdentifier: reuseIdentifier) //Initialize Text Field self.textField = UITextField(frame: CGRect(x: 119.00, y: 9, width: 216.00, height: 31.00)); //Add TextField to SubView self.addSubview(self.textField) } ///------------ //Method: Init with Coder //Purpose: //Notes: This function is apparently required; gets called by default if you don't call "registerClass, forCellReuseIdentifier" on your tableview ///------------ required init(coder aDecoder: NSCoder) { //Just Call Super super.init(coder: aDecoder) } } 

然后在我的UITableViewController类中,我执行了以下操作:

 // File: vcESDEnterCityState.swift import UIKit class vcESDEnterCityState: UITableViewController { //----------------- // MARK: VC FUNCTIONS //----------------- ///------------ //Method: View Will Appear //Purpose: //Notes: ///------------ override func viewWillAppear(animated: Bool) { //First Call Super super.viewWillAppear(animated) //Register the Custom DataCell tvCityStateForm.registerClass(vDataEntryCell.classForCoder(), forCellReuseIdentifier: "cell") } //----------------- // MARK: UITABLEVIEW DELEGATES //----------------- ///------------ //Method: Cell for Row at Index Path of TableView //Purpose: //Notes: ///------------ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //Get Reference to Cell var cell : vDataEntryCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as vDataEntryCell //...Do Stuff //Return Cell return cell } }