Подскажите пожалуйста, что я делаю не так и как правильно вносить изменения в массив items из приведенного ниже примера и обновлять данные в countLabel?
Исходные данные:
Пользовательская ячейка, в которой расположены label и stepper.
class TextCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var stepper: UIStepper!
static let identifier = "TextCollectionViewCell"
static func nib() -> UINib {
return UINib(nibName: "TextCollectionViewCell", bundle: nil)
}
override func awakeFromNib() {
super.awakeFromNib()
}}
Сам ViewController:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
private var items = [0,0,0,0,0,0,0,0,0,0,0]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(TextCollectionViewCell.nib(), forCellWithReuseIdentifier: TextCollectionViewCell.identifier)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TextCollectionViewCell.identifier, for: indexPath) as! TextCollectionViewCell
cell.stepper.tag = indexPath.row
cell.stepper.addTarget(self, action: #selector(addCount(sender:)), for: .valueChanged)
cell.countLabel.text = "\(items[indexPath.row])"
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.bounds.width - 40, height: 100)
}
Я пробовал реализоваться два варианта логики метода @objc func addCount(sender: UIStepper)
-
В качестве cell.stepper.tag я использовал indexPath.row
@objc func addCount(sender: UIStepper) { DispatchQueue.main.async { self.collectionView.reloadData() print(self.items) } let index = sender.tag let count = Int(sender.value) if index == index { items[index] = count }
}
В этом случае массив items обновляется по индексу правильно, только если я не сколлю таблицу.
-
В качестве cell.stepper.tag я использовал максимальную координату ячейки Int(cell.frame.maxY)
cell.stepper.tag = Int(cell.frame.maxY)
Для удобства отступ между ячейками сделал нулевым@objc func addCount(sender: UIStepper) { DispatchQueue.main.async { self.collectionView.reloadData() print(self.items) } let index = sender.tag let count = Int(sender.value) if index == 100 { items[0] = count } if index == 200 { items[1] = count } if index == 300 { items[2] = count } if index == 400 { items[3] = count } if index == 500 { items[4] = count } if index == 600 { items[5] = count } if index == 700 { items[6] = count } if index == 800 { items[7] = count } if index == 900 { items[8] = count } if index == 1000 { items[9] = count } if index == 1100 { items[10] = count }
}
}
В данном случае массив перестает обновляться правильно, начиная с восьмого индекса