【发布时间】: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 的类,它们具有图像和标签。
在代码中,我按数据库填充数组,我想在表格视图中显示它们,但表格视图没有显示任何内容。
【问题讨论】:
-
你在自定义单元格中使用 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