2017-08-14 93 views
0

我正在使用正常的UIViewController中的UITableView。出于某种原因,我的TableView中顶部单元格的文本始终为灰色。为什么顶部tableViewCell灰色?

我一直在试图弄清楚是什么导致该单元变灰,但不知道它是什么。我的tableView中的数据来自于名为的文件列表,该列表在viewWillAppear和viewDidAppear期间都会刷新。

为什么顶部单元格总是灰色的这个代码?

(和它都没事文件列表中,并用文件列表中很多事情发生)

//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

有运行你的代码没有问题,它的运行对我罚款。

即使设置cell.selectionstyle = .grey也不会将灰色添加到单元格或文本。

你必须更清楚的问题。

1)什么是灰色?文字颜色或单元格颜色。

2)你隐藏或做的其他代码是什么?

extension ViewController : UITableViewDelegate,UITableViewDataSource{ 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
     return fileList.count 
    } 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
     let rowData = fileList[indexPath.row] 
     cell?.textLabel?.text = rowData 
     //cell?.isSelected = true 
     cell?.selectionStyle = .gray 
     cell?.isUserInteractionEnabled = true 
     cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator 
     cell?.textLabel?.textAlignment = .left 
     cell?.textLabel?.textColor = UIColor.black // if let priceString = rowData[2] as? String { cell.priceLabel.text = "Price: $(priceString)" } 
     return cell! 
    } 

    // Number of sections in table 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    }// Default is 1 if not implemented 


} 
+0

单元格的文本颜色是灰色的了。还有一堆其他代码 - 它检索用户文档目录中的文件列表(如果启用iCloud Drive,则从应用程序的无处不在容器中),该文件用于填充tableview。除了我在上面分享的扩展之外,没有什么能够对tableview或其单元格做任何事情。 – Grant

+0

@Grant和它的唯一灰色的第一个单元格没有任何其他? –

+0

是的,它只是第一个单元格 – Grant

0

我相信这跟提到的问题here一样。尝试设置:

cell?.textLabel?.isEnabled = true` 
fileList.count == 0 if语句块

cell.isUserInteractionEnabled = false 
相关问题