Swift iOS - Tag Collection View

Swift iOS - Tag collection view

I resolve this problem, using collection view.

class FilterController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

@IBOutlet var collectionView: UICollectionView?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 150, left: 10, bottom: 150, right: 10)
// layout.itemSize = CGSize(width: 90, height: 45)
layout.itemSize = CGSizeFromString("Aloha")

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(TagCell.self, forCellWithReuseIdentifier: "TagCell")
collectionView!.backgroundColor = UIColor.whiteColor()

self.view.addSubview(collectionView!)
}

iOS: Collection view custom tag layout dynamic Height Swift

@iDeveloper I tried to fix your code.

Here are the things you can do.

  1. Simplify your array.

let titles = ["Apple","Google","Computer", "Terminal", "Gross", "Form lands", "Water lands", "river", "mounts", "trees", "places", "parks", "towns", "cities","Lorem Ipsum is simply dummy text of the printing and typesettingindustry.","Hello","A","B","CAD","John","Nick","Lucas","Amy","Lawrance","Apple","Google","Computer", "Browser","Overflow","Hi","Hello","A","B","CAD","John","Nick","Lucas","Amy","Lawrance", "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",""]

Setup a collectionView with tag-like cells

Maybe it will help you. I think you should check if you item stick out the collection view right inset.
In layoutAttributesForElements method check this:

        let collectionViewWidth = self.collectionView?.frame.width - (self.sectionInset.right + self.sectionInset.left)

if (attributes.frame.origin.x + attributes.frame.size.width > collectionViewWidth) {
var newLeftAlignedFrame = attributes.frame
newLeftAlignedFrame.origin.x = self.sectionInset.left
attributes.frame = newLeftAlignedFrame
}

Updated my answer and it works for me, you can see it on screenshot

multiple labels & ImageView in CollectionView iOS

It is better to size items of the collection inside the layout mechanism. So you may want to create a custom FlowLayout and make it divide the available with by 3:

class WeatherCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func prepare() {
super.prepare()

guard let collectionView = collectionView else { return assertionFailure("Collection view not found") }

let availableWidth = collectionView.bounds.width
let availableHeight = collectionView.bounds.height
let itemWidth: CGFloat = (availableWidth/3).rounded(.down)
itemSize = CGSize(width: itemWidth, height: availableHeight)

sectionInset = .zero
minimumLineSpacing = .zero
minimumInteritemSpacing = .zero
}
}

Then you should pass it as the layout of the collectionView.

In Storyboard:

Demo

or Code:

CollectionView(frame: frame, collectionViewLayout: WeatherCollectionViewFlowLayout())

So no need for any fixed width.



Some notes

  1. Don't forget the state where items are more than the desired count!
  2. Don't forget to disable bouncing if you want it to just be fitted!
  3. Don't forget to update the availableWidth (or availableHeight) if you want to play with spacings
  4. Don't repeat yourself! Exclude all repetitive data:
let forecast = self.HourlyData[indexPath.row]

cell.forecastHourlyTemp.text = "\(forecast.temp)°C"
cell.forecastHourlyTime.text = forecast.dt.fromUnixTimeToTime()
cell.forecastHourlyWeatherIcon.image = UIImage(named: forecast.weather[0].icon)

  1. Use lowerCamelcase for everything you define in your code except for types:
HourlyData -> hourlyData

  1. Get rid of self where ever you can!
  2. Get rid of return keyword where ever you can:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { collection.count }

  1. Try NOT to use delegate methods for sizing collection items if you already know the size!

How do I provide tags for my collectionView UITextFields programmatically?

In cellForItemAt delegate set you cell.inputText.delegate = self

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell // REPLACE YOUR CELL NAME WITH CollectionViewCell

cell.inputText.delegate = self
return cell
}

Now you can get tapped textview data by using textViewDidBeginEditing delegate like below.

extension ViewController: UITextViewDelegate { // REPLACE ViewController with Your ViewController Name
func textViewDidBeginEditing(_ textView: UITextView) {
var cell: CollectionViewCell?
cell = textView.superview?.superview as? CollectionViewCell
print(cell?.txtVW.text as! String)
}
}


Related Topics



Leave a reply



Submit