2017-08-14 97 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 
相關問題