Определить последнюю ячейку на полной странице UICell UICollectionView - PullRequest
1 голос
/ 10 февраля 2020

У меня есть процесс проверки с использованием UICollectionViewController и UICell на всю страницу. После отображения последней ячейки (последнего элемента) я хочу добавить сводную страницу со всеми элементами, которые были добавлены в корзину, вместе с общей ценой. Я попытался проверить, если последний элемент = itemsArray.count - 1, однако он продолжает печатать «последний элемент» в ячейке до последнего. Также в последней ячейке выводится «не последний».

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate {

    var totalPrice = Float()

    private var hiddenRows = Set<Int>()

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell

        cell.finalLabel.text = String(totalPrice)
        cell.delegate = self

        let item = itemsArr[indexPath.row]
        cell.set(name: item.name, brand: item.brand, price: item.price)

        if indexPath.row == itemsArr.count - 1 {
            print("last item")
        }else {
            print("not last")
        }
        if hiddenRows.contains(indexPath.row) {
            cell.myButton.isHidden = true
            cell.removeButton.isHidden = false
        }else{
            cell.removeButton.isHidden = true
            cell.myButton.isHidden = false
        }
        cell.finalLabel.text = String(totalPrice)
            return cell
        }

    @objc func addTapped(cell: PostCell) {

        guard let indexPath = self.collectionView.indexPath(for: cell)  else {return}
        hiddenRows.insert(indexPath.row)
        cell.removeButton.isHidden = false
        let item = itemsArr[indexPath.row]
        print(item.price)
        totalPrice += Float(item.price) ?? 0
        cell.finalLabel.text = String(totalPrice)
    }


    @objc func removeButtonTapped(cell: PostCell) {

        guard let indexPath = self.collectionView.indexPath(for: cell)  else {return}
        hiddenRows.insert(indexPath.row)
        cell.myButton.isHidden = false
        let item = itemsArr[indexPath.row]
        totalPrice -= Float(item.price) ?? 0
        cell.finalLabel.text = String(totalPrice)
    }
}



protocol PostCellDelegate {
    func removeButtonTapped(cell: PostCell)
    func addTapped(cell: PostCell)
    func didPressButton(_ tag: Int)
}

class PostCell: UICollectionViewCell {

    var delegate: PostCellDelegate?

    func set(name: String, brand: String, price: String){
        nameLabel.text = name
        brandLabel.text = brand
        priceLabel.text = price
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.myButton.addTarget(self, action: #selector(addButtonTapped(sender:)), for: .touchUpInside)
        self.removeButton.addTarget(self, action: #selector(subButtonTapped(sender:)), for: .touchUpInside)
        setupCellConstraints()
    }

    @objc func buttonPressed(_ sender: UIButton) {
        delegate?.didPressButton(sender.tag)
    }


    @objc func addButtonTapped(sender: UIButton){
        self.delegate?.addTapped(cell: self)
        sender.isHidden = true
    }


    @objc func subButtonTapped(sender: UIButton){
        self.delegate?.removeButtonTapped(cell: self)
        sender.isHidden = true 
    }
}
...