Не могу установить нужных ячеек в collectionView - PullRequest
2 голосов
/ 20 января 2020

This is my CollectionView

У меня есть собственный CollectionViewCell с функцией установки title, description and image, но я не могу понять, как установить его, когда выбрано 1 cell, а секунда - нет , Я использую этот код. Я нахожу центр collectionView и получаю array моего indexPath.item [0,1], но я могу получить доступ к своей камере

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let centerPoint = CGPoint(x: self.collectionView.center.x,  //+ self.collectionView.contentOffset.x,
        y: self.collectionView.center.y) //+ self.collectionView.contentOffset.y)

    let collectionViewCenterPoint = self.view.convert(centerPoint, to: collectionView)


    for cell in collectionView.visibleCells {
        let indexPath = collectionView.indexPathForItem(at: collectionViewCenterPoint)

        switch indexPath?.item {
        case 0:

            break
        case 1:

            break
        case .none:
            break
        case .some(_):
            break
        }
    }
}

это моя камера

class RoleCollectionViewCell: UICollectionViewCell, NiBLoadable {

@IBOutlet weak var circleView: UIView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var roleLabel: UILabel!
@IBOutlet weak var descriptionTextView: UITextView!


override func awakeFromNib() {
    super.awakeFromNib()
    Decorator.decorate(self)
}

func setActive(image: UIImage, borderColor: CGColor) {
    circleView.layer.borderColor = borderColor
    imageView.image = image
}


func setTitle(text: String) {
    roleLabel.text = text
}

func setDescription(text: String) {
    descriptionTextView.text = text
}

}

и это мой метод возврата ячеек

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

    let centerPoint = CGPoint(x: self.collectionView.center.x, y: self.collectionView.center.y)

    let collectionViewCenterPoint = self.view.convert(centerPoint, to: collectionView)

    let models = model[indexPath.row]
    switch models {
    case .client:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RoleCollectionViewCell.name, for: indexPath) as? RoleCollectionViewCell {

            return cell
        }
    case .barber:
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RoleCollectionViewCell.name, for: indexPath) as? RoleCollectionViewCell {
            let activeBarberImage = UIImage(named: "barber_a")
            let deactiveBarberImage = UIImage(named: "barber_b")
            cell.setActive(image: activeBarberImage!, borderColor: burbColor.cgColor)
            cell.setTitle(text: "Barber")
            cell.setDescription(text: "I can make you look real cool")
            return cell
        }

    }
    return UICollectionViewCell.init()
}

Ответы [ 4 ]

1 голос
/ 21 января 2020

Сочетание твоего ответа и ответа @jawadali

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    for cell in collectionView.visibleCells {
        let indexPath = collectionView.indexPath(for: cell)
        // here you get visible cell At index path
        let models = model[indexPath.row]
        switch models {
        case .client:
            if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? RoleCollectionViewCell {

                return cell
            }
        case .barber:
            if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? RoleCollectionViewCell {
                let activeBarberImage = UIImage(named: "barber_a")
                let deactiveBarberImage = UIImage(named: "barber_b")
                cell.setActive(image: activeBarberImage!, borderColor: burbColor.cgColor)
                cell.setTitle(text: "Barber")
                cell.setDescription(text: "I can make you look real cool")

            }

        }

    }
}
0 голосов
/ 22 января 2020

** Я сделал это. оно работает. но теперь все мои клетки становятся активными. Мне нужно, чтобы 1 ячейка была активна, а другая нет. **

    //   MARK: - for switching cells
   func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {


    let centerPoint = CGPoint(x: self.collectionView.center.x, y: self.collectionView.center.y)

    let collectionViewCenterPoint = self.view.convert(centerPoint, to: collectionView)

    let indexPath = collectionView.indexPathForItem(at: collectionViewCenterPoint)

    let cell: RoleCollectionViewCell = collectionView.cellForItem(at: indexPath!) as! RoleCollectionViewCell

    let models = model[indexPath!.item]

    switch models {

    case .client:
        cell.setActive(image: activeBarberImage!, borderColor: burbColor.cgColor)
    case .barber:
        cell.setActive(image: activeClientImage!, borderColor: burbColor.cgColor)
    }

}
0 голосов
/ 20 января 2020

Вы можете получить индекс пути видимых ячеек

 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        for cell in collectionView.visibleCells {
            let indexPath = collectionView.indexPath(for: cell)
            print(indexPath)
            let cell = collectionView.cellForItemAtIndexPath(indexPath) as? yourCustomCellName
            // here you get visible cell At index path
             cell.yourCellMethod()

        }
    }
0 голосов
/ 20 января 2020

Используйте cellForItem(at:), чтобы получить ячейку с определенным indexPath, т.е.

collectionView.cellForItem(at: indexPath)
...