【问题标题】:UITableView dynamic grouped cellsUITableView 动态分组单元格
【发布时间】:2016-06-07 10:24:26
【问题描述】:

我使用静态单元格和分组样式创建了此设置tableview

我想重新创建相同的tableview,但这次我想使用动态单元格。我知道如何使用动态单元格,但我不知道如何设置具有不同标题的不同部分。

我需要 4 个部分(状态、队列、类型、严重性),每个部分都有不定数量的单元格(数量显然等于数据数组的长度)。

有人可以发布一个完整的例子吗?我找不到任何可以帮助我的文档。

【问题讨论】:

  • @Orions 谢谢你,我会试试这个
  • @Orions 提供的教程与 OP 提出的问题无关。它是关于动态单元格的,不讨论节标题或节标题的标题。

标签: xcode swift uitableview


【解决方案1】:

这里是代码TableViewDemo你基本上必须使用以下两种方法。

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 4
}

这个是标题的标题-

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "your title" // Use the titleForHeaderInSection index
}

【讨论】:

  • tableview我只用这两种方法?
  • 好的,是不是按照您的意愿填充了这些部分,您可以发布代码以便我们进行调查吗?
【解决方案2】:

你必须使用numberOfSectionsInTableViewnumberOfRowsInSectiontitleForHeaderInSectioncellForRowAtIndexPath

let section = ["section1", "section2"]
let cellsInSection = [["section1cell1", "section1cell2"], ["section2cell1", "section2cell2"]]

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

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

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section[section]
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.section == 0 {

        let cell = tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! SectionOneTableViewCell

        return cell
    } else if indexPath.section == 1 {

        let cell = tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! SectionTwoTableViewCell
        return cell
    }
}

【讨论】:

  • 感谢您的回答克里斯,我会尝试这种方式。
  • 我刚刚编辑了一些代码。确保您使用更新的。让我知道这是否回答了您的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
相关资源
最近更新 更多