表视图设计问题



我用 TableView 设置了一个页面,我这样做了,当我按下一个单元格时,我会看到"子"单元格打开,如果我再次按下它,它们将再次关闭。 为了便于查看哪些是打开的,我希望它们变成灰色而不是白色,但不幸的是, 一些尚未打开且尚未按下的单元格仍然是灰色的,当我打开应用程序时,它会不时更改哪些单元格被弄乱。

所以我希望有人对如何解决这个问题有一个好主意,或者有一个关于表视图设计的好教程来涵盖这一点?我将粘贴代码中执行颜色更改的部分。

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var dataIndex = indexPath.row - 1
    if indexPath.row == 0 {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
        cell.textLabel?.text = tableViewData[indexPath.section].title
        return cell
    } else {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
        cell.textLabel?.text = tableViewData[indexPath.section].sectionData[dataIndex]
        cell.backgroundColor = UIColor.lightGray
        return cell
    }
}

重要提示:不要添加代码的图像。删除它并在问题中添加代码。这将很容易提供帮助。

现在在代码中,您所做的是向单元格添加颜色:

cell.backgroundColor = UIColor.lightGray

但是,当单元格被重复使用时,您不会将其更改为默认颜色。

默认颜色添加以下代码(在您的情况下为白色(:

cell.backgroundColor = UIColor.white 

最新更新