在Swift中以编程方式创建UITableViewCell

Jim*_*ery 3 uitableview ios swift

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

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

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

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

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

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

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

非常感谢.

zis*_*oft 8

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

  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
}
Run Code Online (Sandbox Code Playgroud)

这应该是基本步骤.

  • 就像我说的 - 我实际上不能使用Interface Builder - 每次我点击一个项目时它会被重新定位并缩小到零.Interface Builder不仅仅让我的生活更加艰难 - 它让我把头发撕掉了.我已经将Xcode升级到了最新版本 - 似乎其他一些人也遇到了这个错误(请参阅我原帖中的链接). (2认同)

小智 7

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

//  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)
}
}
Run Code Online (Sandbox Code Playgroud)

然后在我的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
}

}
Run Code Online (Sandbox Code Playgroud)