ios - 滚动时如何在 UITableView 上保留自定义选择格式

标签 ios swift uitableview select scroll

我有一个 TableView,其中包含可选择的静态单元格(一次可以选择一个),并且我设置了一个自定义“SelectionView”,以便在选择单元格时,其周围会出现蓝色轮廓。

问题是当我滚动或离开 View 并返回(通过转到另一个 View Controller )时,我的自定义格式会以某种方式“破坏”。有时它会消失(滚动时),有时它似乎会删除一半轮廓(当我离开 View Controller 并返回时。)

有没有办法确保我的自定义选择 View 保留在那里?

这是我的代码:

class BackgroundDesignController: UITableViewController {

    let selectionView = UIView()
    let defaults = UserDefaults.standard
    var  selectedBackground = "my default"

    @IBOutlet var myTableView: UITableView!

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if(indexPath == [0,0]){
            BackgroundName = "blank"
            NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
        }
        if(indexPath == [0,1]) {
            BackgroundName = "leaves"
            NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
        }
        if(indexPath == [0,2]) {
            BackgroundName = "water"
            NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
        }
        if(indexPath == [0,3]) {
            BackgroundName = "wood"
            NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
        }          
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if cell.isSelected {
            cell.isSelected = true
        } else {
            cell.isSelected = false
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //customize the selectedBackgroundView
        selectionView.layer.borderColor = UIColor.cyan.cgColor
        selectionView.layer.borderWidth = 6.0 //
        UITableViewCell.appearance().selectedBackgroundView = selectionView
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //let indexPath = IndexPath(row: 1, section: 0)

        //reset the selected background  to the current value of the Background user default
        switch BackgroundName{
        case "blank":
            myTableView.selectRow(at: [0,0], animated: false, scrollPosition: .none)
            self.view.layoutIfNeeded()
        case "leaves":
            myTableView.selectRow(at: [0,1], animated: false, scrollPosition: .none)
            self.view.layoutIfNeeded()
        case "water":
            myTableView.selectRow(at: [0,2], animated: false, scrollPosition: .none)
            self.view.layoutIfNeeded()
        case "wood":
            myTableView.selectRow(at: [0,3], animated: false, scrollPosition: .none)
            self.view.layoutIfNeeded()
        default:
            print("There is a problem: no value for BackgroundName")
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

最佳答案

根据我对您问题的理解,您没有正确重置单元格的状态。 请记住,当您滚动时,表格 View 将重用单元格。因此,这意味着您通常需要在 cellForRowAt 函数中重置它们的状态。 但当您使用静态单元格时,您可能需要在 willDisplayCell 中执行此操作。 如果我们看看您对该函数的实现:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            if cell.isSelected {
                cell.isSelected = true
            } else {
                cell.isSelected = false
            }
        }

这是无稽之谈。如果 isSelectedtrue,则将 true 分配给 isSelected... 所以你这里肯定有问题。

您还可以在 viewWillAppear 中设置单元格的选定状态,但是如果您尝试选择的单元格不可见,会发生什么情况?我可以告诉你:没什么。所以这段代码必须移到其他地方。

这是您尝试做的一个非常简单的行为。我真的建议使用动态单元格,这更容易处理。

关于ios - 滚动时如何在 UITableView 上保留自定义选择格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49620260/

相关文章:

ios - CIGaussianBlur 质量 : Banding & Artifacts

ios - 将弹出窗口添加到以编程方式添加的 UIBarButtonItem 并将图像添加到相同的弹出窗口 Swift

Swift - UITableViewCell 标签位置在滚动后移动

ios - uitableview 单元格有一个横向分隔符

ios - 以编程方式设置/激活 Segue

ios - ARKit float 飞机

ios - 如何从不同的 Xcode 版本复制模拟器?

ios - Swift Core Data - 如何更新所有子实体的属性?

ios - 是否可以通过在实时应用程序中验证收据来获取 21007 状态码?

ios - 如何使用 Swift、iOS 7 设置 rootViewController