У меня есть вид коллекции на мой класс (UICollectionView) и класс PostCell
(UICollectionViewCell). HomeController
имеет всего 4 различных ячейки. У меня также есть кнопка внутри моего PostCell, которая при касании обрабатывает функцию handleChangeSize
. Когда я касаюсь этой кнопки, я хочу, чтобы размер определенных ячеек изменился. Предпочтительно изменять высоту, аналогично функции Instagrams «показать больше» на своих ячейках. Любая помощь будет высоко оценена. Спасибо ... Вот мой код:Нажмите кнопку, чтобы изменить размер ячейки UICollectionView.
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor(white: 0.90, alpha: 1.0)
collectionView?.register(PostCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.showsVerticalScrollIndicator = false
collectionView?.alwaysBounceVertical = true
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = (view.frame.width) * 9/16
return CGSize(width: view.frame.width, height: height + 50 + 50)
}
}
class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
lazy var myButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Change Size", for: .normal)
button.addTarget(self, action: #selector(handleChangeSize), for: .touchUpInside)
return button
}()
func handleChangeSize() {
}
func setupViews() {
addSubview(myButton)
myButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
myButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Я действительно литий ke идея добавить цель, которая указывает на viewcontroller! –
@BartvanKuik спасибо товарищу .. –
вы спасли меня пару часов, пытаясь найти решение самостоятельно! Большое вам спасибо :) @GaneshKumar – MBH