【问题标题】:UITableView doesn't show anythingUITableView 不显示任何内容
【发布时间】:2017-02-28 15:41:40
【问题描述】:

我编写了一个应用,它在 UIViewController 中有一个 UITableView,这是我的代码:

class CategorySelectViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var melliSubCategories = [String]()
var mazhabiSubCategories = [String]()
var sayerSubCategories = [String]()


@IBOutlet weak var melliButton: UIButton!
@IBOutlet weak var sayerButton: UIButton!
@IBOutlet weak var mazhabiButton: UIButton!


@IBAction func melliButtonClicked(_ sender: UIButton) {
    categorySelected = 6
    melliButton.isHighlighted = true
    mazhabiButton.isHighlighted = false
    sayerButton.isHighlighted = false
    categoryTableView.reloadData()
}

@IBAction func sayerButtonClicked(_ sender: UIButton) {
    categorySelected = 5
    melliButton.isHighlighted = false
    mazhabiButton.isHighlighted = false
    sayerButton.isHighlighted = true
    categoryTableView.reloadData()
}

@IBAction func mazhabiButtonClicked(_ sender: UIButton) {
    categorySelected = 4
    melliButton.isHighlighted = false
    mazhabiButton.isHighlighted = true
    sayerButton.isHighlighted = false
    categoryTableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.
    categoryTableView.dataSource = self
    categoryTableView.delegate = self
    categoryTableView.register(CategorySelectTableViewCell.self, forCellReuseIdentifier: "Cell")
    melliSubCategories = DataBaseManager.shared.subCategories(6)
    mazhabiSubCategories = DataBaseManager.shared.subCategories(4)
    sayerSubCategories = DataBaseManager.shared.subCategories(5)
    print(melliSubCategories)
    print("/////////////////")
    print(mazhabiSubCategories)
    print("/////////////////")
    print(sayerSubCategories)
    print("/////////////////")
}

@IBOutlet weak var categoryTableView: UITableView!


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch categorySelected {
    case 4: //mazhabi
        return mazhabiSubCategories.count
    case 5: //sayer
        return sayerSubCategories.count
    case 6: //melli
        return melliSubCategories.count
    default:
        return melliSubCategories.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CategorySelectTableViewCell
    cell.label?.text = melliSubCategories[indexPath.row]

    return cell

}

我为表格视图中的单元格创建了一个名为 CategorySelectTableViewCell 的类,它们具有图像和标签。

在代码中,我按数据库填充数组,我想在表格视图中显示它们,但表格视图没有显示任何内容。

截图:my storyboard,demo

【问题讨论】:

  • 你在自定义单元格中使用 xib 吗?
  • 如果您打印返回的计数值,您会看到什么?如果您打印要放入标签的内容,您会看到什么?
  • @iParesh 不,我不使用 xib,我通过表格视图单元格创建自定义单元格
  • @PhillipMills 所有的打印工作都很好,单元格的数量也是正确的,但是单元格是空的,没有任何标签
  • @AdrianBobrowski print 工作正常,我使用 registerCell 因为 whiteout 它应用程序崩溃并说reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

标签: ios uitableview swift3


【解决方案1】:

你是说:

cell.label?.text = melliSubCategories[indexPath.row]

这是不可能的。要使其正常工作,您的自定义单元格类型 CategorySelectTableViewCell 需要位于带有label 插座的笔尖中。但是那个 nib 要么在故事板中,要么在 xib 文件中。但你也说

categoryTableView.register(CategorySelectTableViewCell.self, ...

那一行阻止单元格来自 xib 或情节提要。因此插座无法工作,电池将保持空置状态。

【讨论】:

  • 我也在考虑... cell.label?.text 怎么可能。最初我以为他使用的是本机单元格,但后来我滚动代码并且它不是本机的。没有得到他实际上在做什么。 ://
  • 如果我删除这行应用程序崩溃并说reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard',但我还设置了原型单元格的标识符
  • 但是您的情节提要配置不正确。您的视图控制器没有正确看到情节提要中的表格视图。
  • 也许您应该发布一个显示相关配置的情节提要的屏幕截图。
  • @matt 截图添加
【解决方案2】:

请在下面的 numberOfRowsInSection 方法中检查数组计数是否为零。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

另外,交叉检查小区标识符是否正确。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell : CategorySelectTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CategorySelectTableViewCell
    cell.label?.text = melliSubCategories[indexPath.row]       
    return cell
}

在 ViewDidLoad 方法中重新加载 tableview:

categoryTableView.reloadData()

override func viewDidLoad() 
{
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    categoryTableView.dataSource = self
    categoryTableView.delegate = self
    categoryTableView.register(CategorySelectTableViewCell.self, forCellReuseIdentifier: "Cell")
    melliSubCategories = DataBaseManager.shared.subCategories(6)
    mazhabiSubCategories = DataBaseManager.shared.subCategories(4)
    sayerSubCategories = DataBaseManager.shared.subCategories(5)
    print(melliSubCategories)
    print("/////////////////")
    print(mazhabiSubCategories)
    print("/////////////////")
    print(sayerSubCategories)
    print("/////////////////")
    categoryTableView.reloadData()
}

【讨论】:

  • 这只是重复OP的问题
  • @AshleyMills OP 是什么?
  • 原始海报。你没有给出答案——你只是重复了他的问题。他已经澄清了你加薪的要点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 2020-11-10
  • 2012-04-12
  • 2019-11-11
  • 2017-09-25
  • 2012-08-02
  • 2019-08-11
相关资源
最近更新 更多