ios - 更改表格第一个单元格的背景图像
问题描述
我使用表格视图创建了一个记分板,我想突出显示表格的第一行。当我运行应用程序时,第一行按预期突出显示,但是当我滚动表格时,突出显示了几个单元格而不是第一个单元格。
这是我的代码;
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? String
cell.scoreLabel.text = " \(indexPath.row + 1)"
return cell
}
解决方案
单元格出列意味着当您cellForRow
被要求输入单元格(这不是第一个)时,返回的单元格可能会使第一个单元格出列,因此请确保nil
通过替换来设置它不是索引为零的时间
if indexPath.row == 0 {
cell.backgroundImageView.image = UIImage(named: "firstClass")
}
和
cell.backgroundImageView.image = indexPath.row == 0 ? UIImage(named: "firstClass") : nil
推荐阅读
- azure-active-directory - Azure AD B2C 作为 Azure AD 的声明提供者或身份提供者
- javascript - Graphql/React-Apollo:反应地图功能错误
- html - 通过在 SVG 中使用 CSS 变量
- excel - 使用 VBA 将边框应用于单个单元格
- wordpress - AWS EFS 上的 Wordpress 媒体上传问题
- vue.js - 浅山深一层
- c# - 从字符串中删除隐藏的字符
- c++ - 使用类模板的内部类模板专门化模板
- python - 我想将我的应用程序提供给某人,但它在他的计算机上不起作用 Python pyinstaller
- visual-studio-2015 - Visual Studio 2015 在哪里存储其 Git 配置信息?