iOS:TableView 中的多个部分不起作用

标签 ios swift uitableview

我正在开发一个应用程序,我需要在其中显示带有标题的多行。在我的例子中,只显示了一个部分。我搜索了所有内容,但找不到合适的解决方案。

这是我的代码:

class Timeline: UITableViewCell {
      @IBOutlet weak var timelineData: UITextView!
}

class StudenTimelineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let section = ["pizza", "deep dish pizza", "calzone"]

let items = [["Margarita", "BBQ Chicken", "Peproni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns & mashrooms"]]

override func viewDidLoad() {
    super.viewDidLoad()
}

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

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineId", for: indexPath) as! Timeline
    let gpsData = items[indexPath.section][indexPath.row]

    cell.timelineData.text = gpsData
    return cell
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}

我得到了什么 enter image description here

我将如何获得所有部分。提前致谢。

最佳答案

这是因为您的方法名称 func numberOfSectionsInTableView(tableView: UITableView) -> Int 不正确,因此未被调用。

func numberOfSections(in tableView: UITableView) -> Int 替换名称,看看奇迹发生了。

关于iOS:TableView 中的多个部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52621491/

相关文章:

ios - MPMediaItemArtwork 和 UITableView

ios - TableView Segue 到新 ViewController 不能工作两次

ios - 每行 3 个项目的 UICollectionViewCell 大小

swift - (Swift) 选项卡栏项目 : Custom Selected Image with Rendering asOriginal is Not Showing

swift - 从 UITableViewCell 类中点击 UITextView 中的主题标签后尝试继续

swift - 如何从 JSON 中过滤多个值并将结果附加到 TableView Swift 4

iOS 如何在 UITextView 中内联插入 UITextField

swift - 如何检测cgRect中的cgPath

ios - Swift:XCTest 类 'FirstDemoTests' 没有初始化器

ios - UITableViewCell 内的 UIWebView,检测到链接但不可点击