I am building a filter function, where you can select your desired users to see their reports. The section is almost done, but there is a bug where when I scroll fast up and down, some of the selected cells are getting unselected and some of the unselected cells are selected.
The UserCell class consists of an UIView, an UILabel and an UISwitch. How can I fix the bug so when I scroll up and down, the cells keep their selections?
EDIT:
Here are some screenshots. The first one is when I toggled 5 and the second one is when I scrolled down (18 at the bottom is not a bug).
import UIKit
class BaseCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class UserCollectionView: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "userCellId"
var users = [User]()
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.alwaysBounceVertical = true
cv.dataSource = self
cv.delegate = self
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
override func setupViews() {
super.setupViews()
users = UserDefaults.standard.getUsers() != nil ? UserDefaults.standard.getUsers()! : [User]()
self.addSubview(collectionView)
setupCollectionView()
}
func setupCollectionView() {
collectionView.register(UserCell.self, forCellWithReuseIdentifier: cellId)
collectionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
collectionView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
collectionView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return users.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) as! UserCell
cell.user = users[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.frame.width, height: 40)
}
}