this tutorial i want to show you how to use "UICollectionView " in swift

ready to get started and go to File->New->Project and select iOS->SingleView Application

click Next to fill out information about the application Set the Product Name "Colectionview"


ready to get started and go to File->New->Project and select iOS->SingleView Application

click Next to fill out information about the application Set the Product Name "Colectionview"

Device family :you can chose universal it’s not important
Choose swift as Language
Click Next to select the project location, and then click CreateStarting your Colectionview:
Next, select the Collection View Cell and set the identifier as “Cell” in the Attribute Inspector.
and open the Collection View cell and control - drag form lableview and make outlet named
then control drag from Imageview and make outlet names myImageview
Close Assistantant editor and going to Viewcontroller.file
It need delegate and datasourse in the “viewdidload” above we set both the delegate and dataSource to to self so we can add the required methods here change the class declaration to this :
class ViewController: UIViewController ,UICollectionViewDataSource,UICollectionViewDelegate{
Add some data to display:Add a property called items as an Array of Strings and set some values var imageview:[String] = ["a.jpg","c.jpg","E.jpg","jp.jpg"]
var detail:[String] = ["America","Canada","England","Japan"]
after that I need to element:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageview.count
}
Create the cell:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CollectionViewCell
cell.myImageview.image = UIImage(named: imageview[indexPath.row])
cell.lblView.text = detail[indexPath.row]
return cell
}
Then, Build and Run the application you will get the output like below.
so this end of my blog i hope that it help you learn a lot please post any question and comment right here so that i can support for you
i'm Kevin see you next time
var detail:[String] = ["America","Canada","England","Japan"]
after that I need to element:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageview.count
}
Create the cell:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CollectionViewCell
cell.myImageview.image = UIImage(named: imageview[indexPath.row])
cell.lblView.text = detail[indexPath.row]
return cell
}
Then, Build and Run the application you will get the output like below.
so this end of my blog i hope that it help you learn a lot please post any question and comment right here so that i can support for you
i'm Kevin see you next time








Comments
Post a Comment