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


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
}  
      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个自定义单元格,试试这个

每个Section都包含行,因此当UITableViewDelegate请求cellforRowAtIndexPath时,需要以下结构:

Section 1
  Row1
  Row2
  Row3
Section 2
  Row1
  Row2
...

所以每个Section包含一些行,每个行/Section可以有不同的单元格布局/类。

所以你不需要每个单元格的xib布局(当使用故事板)-只是在你的UITableViewUITableViewController内做一些不同的布局,并将你的IBOutlets连接到特定的UITableViewCell类。

现在您应该有如下结构,如上所述。
let cellObjects = [ClassForSection: [ClassForRow]]()

那么你可以使用:

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

:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
  let currentRowItem = cellObjects[indexPath.section][indexPath.row]
  // so you have an specific object for every cell
}

最新更新