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

标签 swift uitableview 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 文件。

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

建议?非常感谢。

编辑:

我决定直接在表格 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/

相关文章:

ios - 使用 Storyboard的 DidSelectRowAtIndexPath

uitableview - TableView numberOfSections 方法未在 swift3 中调用

iphone - 更新应用程序后加载错误的 xib

ios - Objective-C ,线程 1 程序接收信号 SIGABRT

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

未保存整数的 IOS Swift 值

ios - 在单击单元格之前,UITableView 无法正确滚动到底部

ios - 当我在 Parse 中创建任何对象时,createdAt 属性不是正确的创建时间。

ios - UITableView 滞后于大图像

iphone - 如何设计一个同时适用于横向和纵向模式的 UIview?