пользовательская ячейка и пользовательская высота для каждого - PullRequest
0 голосов
/ 15 февраля 2019

Я вывожу 4 пользовательских ячейки с помощью функции переключения, теперь мне нужно вывести либо динамическое значение высоты, либо фиксированное значение высоты для каждого из них.Мне нравятся оба, так как их высота всегда статична.

Я подписался на протокол uiСollectionviewВelegateFlowLayout и нашел, как изменить высоту всех ячеек

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let size = CGSize(width: 357, height: 600)
        return size

, чтобы переопределить рост отдельныхЯ использую функцию

cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0)

, но она не работает, "Тип выражения '@lvalue CGRect' является неоднозначным без дополнительного контекста"

Что мне делать в этом случае?какую функцию использовать?

полный код ниже

// main protocols UICollectionView
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    // width and height for cell in collectionView
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let size = CGSize(width: 357, height: 600)
        return size
    }


    //margins for cell in collectionView
    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 40.0
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

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


            var cell: UICollectionViewCell!

            switch indexPath.row {
            case 1:
                cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath)
                    as? secondCollectionViewCell
                cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0)

            case 2:
                cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath)
                    as? cellThirdCollectionViewCell

            case 3:
                cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fourthCell", for: indexPath)
                    as? fourthCollectionViewCell

            default:
                cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
                    as? imageModelCollectionViewCell
            }
            return cell
        }
    }

enter image description here

Ответы [ 3 ]

0 голосов
/ 17 февраля 2019
extension ViewController : UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        switch indexPath.row {
        case 1:
            return CGSize(width: 100, height: 200)
        case 2:
            return CGSize(width: 100, height: 300)
        case 3:
            return CGSize(width: 100, height: 400)
        default:
            return CGSize(width: 100, height: 100)
        }
    }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        var cell: UICollectionViewCell!

        switch indexPath.row {
        case 1:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath)
                as? firstCollectionViewCell


        case 2:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath)
                as? secondCollectionViewCell

        case 3:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath)
                as? thirdCollectionViewCell

        default:
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
                as? imageModelCollectionViewCell
        }
        return cell
    }
}

полностью решает проблему, не забудьте добавить

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
}

ps. Если вы получаете сообщение об ошибке "collectionView.delegate = self", это может быть причиной того, что вы работаете в viewController инет в коллекции ViewController

Вам нужно добавить IBOutlet

0 голосов
/ 17 февраля 2019

Вы пытались изменить свою функцию func collectionView(_:layout:sizeForItemAt:) на что-то подобное?

let cellHeights = [ 90, 400, 100, 70 ]

// width and height for cell in collectionView
func collectionView( _ view: UICollectionView, 
                     layout collectionViewLayout: UICollectionViewLayout,
                     sizeForItemAt indexPath: IndexPath ) -> CGSize 
{
    return CGSize( 
        width: view.bounds.size.width, 
        height: cellHeights[indexPath.row] 
    )
}

Кроме того, приведение результата collectionView.dequeueReusableCell(withReuseIdentifier:for:) к подклассу UICollectionViewCell не влияет на ваш код ...

Я мог бы изменить его на что-то вроде:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
    // map row numbers to cell reuse identifiers...   
    let ids = [ 
        0: "secondCell", 
        1: "thirdCell", 
        2: "fourthCell" 
    ]

    // look up the reuse identifier for the row we want; if none found (subscript call returns `nil`), use `"imageCell"`
    let reuseID = ids[indexPath.row] ?? "imageCell"
    let cell = collectionView.dequeueReusableCell( withReuseIdentifier: reuseID, for: indexPath)

    return cell
}
0 голосов
/ 15 февраля 2019

Вы можете использовать массив высоты и получить высоту ячейки на основе indexPath.

let heightArray = [90,400,100,70]

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

                return CGSize(width: 357, height: heightArray[indexPath.row])

        }
...