【问题标题】:This code does not show any table view why?这段代码没有显示任何表格视图,为什么?
【发布时间】:2016-12-03 12:44:05
【问题描述】:

我在 main.storyboard 中放了一个UINavigationController 并将其命名为“TableViewController”,并将原型单元格命名为“Cell”。一切都很清楚,构建成功,但没有任何东西可以显示表格视图,而是显示一个白色的空白表格视图。请建议我一个解决方案或您的想法。
这是 ViewController.swift 中的代码:

import UIKit

class TableViewController: UITableViewController {


struct Objects {
    var sectionName : String!
    var sectionObjects : [String]!
}

var objectsArray = [Objects]()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    objectsArray  = [Objects(sectionName: "General", sectionObjects: ["150/23","A: 98","W: 76","B: 89", "Quantity: 5kg"]),Objects(sectionName: "General", sectionObjects: ["150/23","A: 98","W: 76","B: 89", "Quantity: 5kg"]),Objects(sectionName: "General", sectionObjects: ["150/23","A: 98","W: 76","B: 89", "Quantity: 5kg"]),Objects(sectionName: "General", sectionObjects: ["150/23","A: 98","W: 76","B: 89", "Quantity: 5kg"])]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


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

    let cell = tableView.dequeueReusableCell(withIdentifier:"Cell") as UITableViewCell!

    cell?.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]

    return cell!


}

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

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return objectsArray.count

}

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

【问题讨论】:

  • swift 3.0 和 2.0?
  • 你的细胞正在制造吗?检查调试视图层次结构。
  • 我猜你的意思是UITableViewController,而不是UINavigationController
  • swift 3.0 @HimanshuMoradiya
  • @shallowThought 我该怎么办?用类第 2 行中的 UINavigationViewcontroller 替换 UITableviewcontroller?我替换它给出了错误

标签: ios swift uitableview tableview


【解决方案1】:

这是因为您的实现没有覆盖正确的方法。改变后

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

为我工作。

【讨论】:

  • 感谢您的建议,但它仍然无法正常工作,只是白色的空白表格视图我想要打印我的对象。
  • 我能想到的另一件事是:“我在 main.storyboard 中放置了一个 UINavigationController,并将其命名为“TableViewController”,并将原型单元格命名为“Cell”。”您应该更改 UITableViewController 的类而不是 UINavigationController。如果不是这种情况,那么我不知道,因为我记下了您的代码(通过我的小修复)并且它可以工作。
  • 感谢您的努力,但还是一样
  • @Xcodian Solangi 用于开始检查是否调用了方法。如果不是 - 检查表委托和数据源是否设置正确(例如在 viewDidAppear 上打印 table.delegate)。我在上面的代码中没有看到设置这些属性,但它们也可以从 xib/storyboard 或类扩展中设置。
猜你喜欢
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2016-02-05
相关资源
最近更新 更多