gpt4 book ai didi

swift - 如何在 Swift 的 TableviewController 的多个部分中插入多个自定义单元格(每个 xib 文件一个)?

转载 作者:行者123 更新时间:2023-11-28 08:26:49 30 4
gpt4 key购买 nike

struct Objects{
var section: String! //Section name
var cellIndex: [String] //cell index
}
var objects = [Objects]()

override func viewDidLoad() {
super.viewDidLoad()
objects = [Objects(section: "Profile", cellIndex: ["name", "gender"]), Objects(section: "Change Login", cellIndex: ["changeLogin"])]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objects.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return objects[section].cellIndex.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
if objects[indexPath.row].cellIndex == "name" {
let cell = tableView.dequeueReusableCellWithIdentifier("profileNameCell") as!ProfileNameCell
return cell
}else
if objects[indexPath.row].cellIndex == "gender" {
let cell = tableView.dequeueReusableCellWithIdentifier("profileGenderCell") as!ProfileGenderCell
return cell
}else{
objects[indexPath.row].cellIndex == "changeLogin" {
let cell = tableView.dequeueReusableCellWithIdentifier("profileChangeLoginCell") as!ProfileChangeLoginCell
return cell
}

每个“ProfileNameCell”、“ProfileNameCell”、“ProfileChangeLoginCell”都连接到一个 xib 文件。

上面的代码无法编译。我阅读了代码示例并尝试进一步研究我的代码,但我不知道如何插入这些部分。

建议?非常感谢。

编辑:

我决定直接在表格 View 中创建单元格,就像 Vadian 所说的那样,并且成功了。我之前尝试过,但是单元格组件没有连接到 UiTableViewController。由于这个问题,我决定使用 xib 文件。现在元素正在连接。为了显示所有单元格和部分,我使用了以下代码:

struct Cells{
var section: String!
var rows: [String]!
}
var tableStructure: [Cells] = []

override func viewDidLoad() {
super.viewDidLoad(
tableStructure = [Cells(section: "One", rows: ["1", "2", "3", "4", "5"]), Cells(section: "Two", rows: ["1"]), Cells(section: "Three", rows: ["1", "2"])]
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return tableStructure.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableStructure[section].rows.count
}

最佳答案

      var cellIndex: String //cell index



objects = [Objects(section: "Profile", cellIndex: ["name"]),Objects(section: "Profile", cellIndex: ["gender"]), Objects(section: "Change Login", cellIndex: ["changeLogin"])]




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

如果你想要 tableview 的 3 个自定义单元格,试试这个

关于swift - 如何在 Swift 的 TableviewController 的多个部分中插入多个自定义单元格(每个 xib 文件一个)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39776076/

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