ios - 对于以编程方式创建其所有内容的 UITableViewCell,我应该使用哪些初始化程序/方法?

标签 ios swift uitableview resource-management

我有一个我实际创建的自定义表格 View 单元格。 不使用 IBOutlet。

我想在界面生成器和代码中使用它。

我想在静态 TableView Controller 的界面生成器中使用它。

在动态 TableView Controller 的代码中

我不会使用 .xib 文件。

经过几天的阅读,这是我到目前为止所学到的。

// This method will be called when I create the cell programmatically 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    commonInit()
}


// This will be called when a file is loaded from storyboard (or .xib)
// will not use xib only storyboard file (static table view cell)

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}


// this will be called after init(coder) 
// when all IBOutlets are ready for use
// I'm currently not using this method
// because I don't have any IBOutlets 
// Not sure if I should use it

override func awakeFromNib() {
    super.awakeFromNib()
}

一切似乎都运行良好。

在代码中使用单元格时,我手动注册单元格并重用它。

这段代码好吗? 我似乎无法找到任何关于一切究竟如何运作的明确解释。我阅读了网上能找到的所有内容,但我不断发现不同的意见和想法。 我想要清楚地解释何时应使用每个初始化方法。

例如,一些在线代码使用 awakeFromNib 和 init(coder) 方法。他们似乎在这两个方法中都调用了commonInit()。 我真的看不出有什么理由,因为它们是一个接一个地被调用的。

最佳答案

完全在代码中创建和使用 UITableViewCell 并不难。 我将给出一个您可以创建的最简单单元格的示例。您可以阅读文档以查看其他更复杂的单元格配置(带有图像、字幕等)

在类的顶部(左大括号之后),您应该为新单元格定义一个标识符,该标识符与您在 Storyboard中使用的标识符不同。

let cellIdentifier = "Cell"

在 viewDidLoad 中,您需要注册新单元格:

 tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)

然后,当您需要一个单元格时,您可以使用之前定义的标识符来请求它:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
    cell?.textLabel?.text = dataSource[indexPath.row].title
    return cell!
}

最后:

当使用 nib/xib 中的单元格而不是纯粹从代码中使用单元格时,您需要进行的唯一更改是:

let cellNib = UINib(nibName: "DropDownCell", bundle: Bundle(for: DropDownCell.self))

从磁盘加载单元格,而不是简单地加载初始化程序,当您注册单元格时,您就注册了刚刚创建的 Nib :

        tableView.register(cellNib, forCellReuseIdentifier: cellReuseIdentifier)

Apple Swift 文档始终是了解如何使用事物的最佳起点。 Swift 3 , "Swift and Cocoa"

Matt Neuberg(他在这个网站上非常活跃)写了很多我买过的书,他的“iOS 编程”系列有一章是关于使用 Nib 中的单元格配置布局的。

还有一个 Swift 在线类(class),HackingWithSwift ,它是免费的,并且有一章介绍 UITableViewCells。它向您展示了如何对单元格中的文本进行着色,使其显得更加流行。 UITableViewCells

许多人都对这里的教程深信不疑:Ray Wenderlich这些的好处是您可以下载代码之前和之后的快照,这样您就可以看到一切正常运行。

这是使用自动布局自动调整单元格大小的一个 Wenderlich - Autolayout Cells

当你真的陷入困境时[你已经用谷歌搜索但仍然无法弄清楚],你可以来这里。尝试提出非常具体的问题,并举例说明您需要什么,并展示您已经尝试过的内容。我想你会惊讶地发现,当你清楚地知道图片中缺少什么时,有人会如此 swift 地帮助你。

我认为我在这里有点过分了,因为很明显你知道自己在做什么......继续坚持,🍀!

关于ios - 对于以编程方式创建其所有内容的 UITableViewCell,我应该使用哪些初始化程序/方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341587/

相关文章:

swift - swift 中的 Coreplot : change starting value of axis to 1

ios - 以编程方式创建与点击按钮关联的标签

swift - 在同一个 View Controller 中使用 2 个 TableView (Swift 4)

iphone - 在 UItableview 中添加一行

ios - 无法从委托(delegate)方法中关闭 FBfriendPickerViewController

ios - Obj-C 框架返回 nil,并使我的 Swift 代码崩溃并显示 'fatal error: unexpectedly found nil while unwrapping an Optional value'

ios - 创建跨类的全局对象

ios - CNContactStoreexecuteSaveRequest 失败,CNErrorDomain Code=101 No Accessible Writable Containers

iphone - UITableView 搜索后应用程序崩溃

ios - 从单个 TableView 实例化多个 View Controller