3

How can I change the X position of every single cell programmatically? I can change the width and height of the cell as my wish but it is unable to complete the project because of XY position.

  • My result ,

    enter image description here

  • Expected result is,

enter image description here

and my full view controller code is,

import UIKit

class ReviewVC: UIViewController , UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{


    var cellWidthArr : [CGFloat] = [60.0 , 170.0 , 110.0 , 120.0]


    var titleArr = ["  Polite  " , "  Showed up on time  ", "  Quick responses  " , "  Fair prices  "]

    override func viewDidLoad() {
        super.viewDidLoad()


        self.navigationItem.title = "Review your experiance"

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView!.collectionViewLayout = layout

    }


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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReviewCollectionCell", for: indexPath)as? ReviewCollectionCell

        cell?.titleLabel.text = self.titleArr[indexPath.row]

        cell?.titleLabel.layer.cornerRadius = (cell?.titleLabel.bounds.size.height)!/2
        cell?.titleLabel.clipsToBounds = true

        cell?.titleLabel.layer.borderColor = (UIColor.init(red: 240/255, green: 36/255, blue: 70/255, alpha: 1.0)).cgColor
        cell?.titleLabel.layer.borderWidth = 1.0

        return cell!
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: self.cellWidthArr[indexPath.row], height: 30)
    }
}

Anyone please help me to findout the solution.Thanks...

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43

2 Answers2

2

You can use the open source project TagCellLayout written by riteshhgupta in GitHub.

Sample Code to initialize the layout:

import TagCellLayout

let tagCellLayout = TagCellLayout(alignment: .left, delegate: self)
collectionView.collectionViewLayout = tagCellLayout

enter image description here

I know you might write the lots of code for this, but it would be helpful greatly and even you can customize to different layout alignments.

Sateesh Yemireddi
  • 4,289
  • 1
  • 20
  • 37
1
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return (ArrayName[indexPath.row] as NSString).size(withAttributes: nil)
}

Rather than giving Cell Fixed Size in an Array there is a way to find label size in sizeForItemAtIndexPath return the size of the text So it will find the size and work

Ruban4Axis
  • 821
  • 6
  • 27
  • Hi thanks for your answer. But while execute the codes the labels are overlapping each others I think need some more helps...please – Angel F Syrus Dec 05 '18 at 06:51