我正在使用普通UIViewController中的UITableView。出于某种原因,我的TableView中顶部单元格的文本始终为灰色。
我一直试图找出导致细胞变灰的原因,但不知道它是什么。我的tableView中的数据来自名为 fileList 的数组,该数组在viewWillAppear和viewDidAppear期间都会刷新。
为什么这个代码顶部单元格总是灰色的?
(它确实发生在fileList中没有任何内容,并且在fileList中有很多东西)
//MARK: - TableView
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if fileList.count == 0 {
return 1
} else {
return fileList.count
}
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if fileList.count == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.isUserInteractionEnabled = false
cell.accessoryType = UITableViewCellAccessoryType.none
cell.textLabel?.text = "No documents found"
cell.textLabel?.textAlignment = .center
cell.textLabel?.textColor = UIColor.black
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.isUserInteractionEnabled = true
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text = (fileList[indexPath.row])
cell.textLabel?.textAlignment = .left
cell.textLabel?.textColor = UIColor.black
return cell
}
}
}
答案 0 :(得分:0)
运行代码没有问题,它对我运行正常。
即使设置TabStrip1_Change()
也不会将灰色添加到单元格或文本中。
你必须更清楚问题。
1)灰色是什么?文字颜色或单元格颜色。
2)你隐藏或分开的代码是什么?
cell.selectionstyle = .grey
答案 1 :(得分:0)
我认为这与提到的here问题相同。尝试设置:
cell?.textLabel?.isEnabled = true`
在行之后的fileList.count == 0
的if语句块中的
cell.isUserInteractionEnabled = false