使用Swift编程以程序化方式创建UITableViewCell

3

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

首先,由于我遇到了Xcode中的这个错误变体,所以无法使用Interface Builder。每次我在Interface Builder中点击一个元素时,该视图中的所有内容都会获得零高度和宽度,并被重新定位到视图之外。此外,我想学习如何通过编程实现此目标。

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

第三我发现了这个视频,但尽管它相当难以理解(很多代码只是复制粘贴而没有对其进行解释或说明其作用),但最终仍然使用了Interface Builder来更改各个部分。

我已经成功设置了一个基本的UITableView。我只想能够向该表视图添加自定义单元格。

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

  • 有人可以指点我方向或帮助我在Swift中以编程方式创建自定义单元格吗?

非常感谢。

2个回答

8

如果你需要更多的代码,这里有一个我创建的自定义单元格的示例:

//  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
}

}

1
这段代码中的tvCityStateForm是指TableView还是View?tvCityStateForm.registerClass(vDataEntryCell.classForCoder(), forCellReuseIdentifier: "cell")。 - Lydia

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
}

这应该是基本步骤。

谢谢你的回答 - 它非常有帮助!然而,我在注册自定义单元格类时卡住了。自定义单元格类坚持要有这个初始化器 required init(coder aDecoder:NSCoder),我似乎无法在没有它的情况下初始化单元格,如果我只是尝试使用init(style: reuseIdentifier:),Xcode也会出现错误。我需要在这里使用什么NSCoder - Jimmery
嗯,我想我也卡在这里了。我可以问一下,为什么你真的想通过代码来完成所有这些,而 InterfaceBuilder 可以让你的生活更轻松呢? - zisoft
2
就像我说的一样 - 我真的无法使用Interface Builder - 每次我点击一个项目,它都会被重新定位并缩小到零大小。 Interface Builder不仅让我的生活更加困难 - 它还让我抓狂。我也将Xcode升级到了最新版本 - 看起来其他一些人也遇到了这个错误(请参见我原始帖子中的链接)。 - Jimmery
也许使用 AppCleaner 完全卸载 Xcode 会有所帮助。 - zisoft
值得一试...我在XCode上没有这样的问题。我同时使用6.1和6.2 Beta。 - zisoft
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接