【发布时间】:2016-09-29 17:00:55
【问题描述】:
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文件。
上面的代码不能编译。 我阅读了代码示例,并尝试进一步了解我的代码,但我不知道如何插入这些部分。
建议?非常感谢你。
编辑:
我决定直接在表格视图中创建单元格,就像 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
}
【问题讨论】:
-
您知道您可以直接在表格视图中创建多个自定义单元格吗?不需要笔尖。并且不要保留任何单元格索引。你肯定会遇到问题。
-
多个会话或部分?
-
"Sections",对不起我的错误。
标签: swift uitableview xib