【问题标题】:How insert multiple custom cells (one for each xib file) in multiple sections in a TableviewController in Swift?如何在 Swift 的 TableviewController 的多个部分中插入多个自定义单元格(每个 xib 文件一个)?
【发布时间】: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


【解决方案1】:
      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
 }

如果您想要 3 个自定义单元格作为您的表格视图,请尝试这个

【讨论】:

    【解决方案2】:

    每个Section都包含Rows,所以当UITableViewDelegatecellforRowAtIndexPath中询问时,需要如下结构:

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

    所以每个部分都包含一些行,并且每个行/部分都可以有不同的单元格布局/类。

    因此,您不需要为每个单元格设置 xib 布局(使用 Storyboard 时) - 只需在您的 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
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      相关资源
      最近更新 更多