我使用表格視圖創建了一個積分板,我想突出顯示表格的第一行。當我運行該應用程式時,第一行被高亮顯示,但當我滾動表格時,幾個單元格被高亮顯示,而不是第一個單元格。
以下是我的代碼;
我的代碼如下
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView. dequeueReusableCell(withIdentifier: "score", for: indexPath) as! ScoreTableViewCell。
if indexPath.row == 0 {
cell.backgroundImageView.image = UIImage(named: "firstClass")
}
cell.nicknameLabel.text = self.sortedResults[indexPath.row]["nickname"] as?
cell.scoreLabel.text = " (indexPath.row 1)"
return cell
}
uj5u.com熱心網友回復:
單元格是去排隊的,這意味著當你的cellForRow被問及單元格(這不是第一個單元格)時,回傳的單元格可能會去排隊,所以當它不是索引為零時,請確保設定nil來替換
if indexPath.row == 0 {
cell.backgroundImageView.image = UIImage(named: "firstClass")
}
與
cell.backgroundImageView.image = indexPath. 行 == 0 ? UIImage(named: "firstClass") : nil
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/311699.html
標籤:
