gpt4 book ai didi

ios - 如何在 UITableView Swift 3.0 中返回不同的 View

转载 作者:行者123 更新时间:2023-11-28 21:11:23 24 4
gpt4 key购买 nike

我有一个包含一个部分的 TableView ,它由一个包含 4 个对象的数组填充。我想在最后一行加载一个文本字段,但我不知道如何在 cellForRowAt indexPath: _) 中返回两个不同的单元格。我可以创建 2 个原型(prototype)单元格,给它们不同的标识符,但如果我使用 if else,将只返回一个单元格。

  var options = [
Item(name:"Doorman",selected: false),
Item(name:"Lockbox",selected: false),
Item(name:"Hidden-Key",selected: false),
Item(name:"Other",selected: false)
]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return options.count
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}


// configure the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
var cell: UITableViewCell
if indexPath.row <= 4 {
cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
cell?.textLabel?.text = options[indexPath.row].name
return cell!
} else {
cell = tableView.dequeueReusableCell(withIdentifier: "textField")
return cell!

}

这就是我要实现的目标。 enter image description here

最佳答案

你的想法没问题,你使用了两个不同的标识符,但我还看到你在 options 数组中只有 4 个项目。然后在 cellForRowAt 中,您有

if indexPath.row <= 4 {

所以它永远不会进入else。您需要将其更改为:

if indexPath.row < 3 {

关于ios - 如何在 UITableView Swift 3.0 中返回不同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43036417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com