Collection view loading only one cell at a time - swift

Edited
I am using custom collection cells to load in collectionview. But when the collection view loadeds expecting behaviour cellforindex delegate will call for 3 indexpath but in this case only calling 0th indexpath 3 time.
override func viewDidLoad()
{
super.viewDidLoad()
let nib = UINib(nibName: "cellName", bundle: nil)
self.collectionvView?.register(nib, forCellWithReuseIdentifier: "cellId")
}
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 10
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
print("indexpath.row ----------------- ",indexPath.row)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId",for: indexPath) as! DetailCollectionViewCell
return cell
}

Check All This
self.arrIds.count = 3
numberOfSections = 1
numberOfItemsInSection
cellForItemAt

Related

my xcode version is 13 and not support the collectionView below method

import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var shoppingapp:[String] = ["cher","sale Small","shoes"]
override func viewDidLoad() {
super.viewDidLoad()
}
//this function is not working in my xcode 13 not calle this function
# func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
# return shoppingapp.count
# }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionCell
cell.shoppinfappimage.image = UIImage(named: shoppingapp[indexPath.row] + ".jpeg")
cell.shoppinfappimage.layer.cornerRadius=50.0
return cell
}
}
I don't know which other method is there I am learning and practical in iOS application development****
First, please remove # from the code. Divide him into the ViewController extension.
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return shoppingapp.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionCell
cell.shoppinfappimage.image = UIImage(named: shoppingapp[indexPath.row] + ".jpeg")
cell.shoppinfappimage.layer.cornerRadius=50.0
return cell
}
}
But when I saw your code, I think you didn't declare delegate and DataSource.
//in viewDidLoad
collectionView.delegate = self
collectionView.dataSource = self
Write the code above in viewDidLoad. So I couldn't do a lot either.
If it didn't work, try it like this.
#available(iOS 13.0, *)
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return shoppingapp.count
}
I hope the writing helps me.

Change `numberOfItemsInSection` parameter of a collection view

How is it possible to change numberOfItemsInSection parameter of a collection view?
I have made a basic setup of a collection view. And now I try to make a button, that changes the amount of items in it.
The general setup is a standard one:
func numberOfSections(in collectionView: UICollectionView) -> Int {
1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
arrayA.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imageView.image = arrayA[indexPath.item][0].image
return cell
}
The question is - how to configure a button, so it could change numberOfItemsInSection parameter from current arrayA.count to some other (e.x. arrayB.count)?
Example:
You could take a common a flag to toggle between arrayA and arrayB. When the button is clicked as
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return isUseArrayA ? arrayA.count : arrayB.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imageView.image = isUseArrayA ? arrayA[indexPath.item][0].image : arrayB[indexPath.item][0].image
return cell
}
#IBAction func changeSource(sender: UIButton) {
if sender.tag == 0 {
isUseArrayA = true
sender.tag = 1
} else {
sender.tag = 0
isUseArrayA = false
}
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.collectionView.reloadData()
}
}
// No need of numberOfSection because it is bydefault 1.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
arrayA.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imageView.image = arrayA[indexPath.item][0].image
return cell
}
#IBAction func changeArrayCount(sender: UIButton) {
arrayA.append(image)
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.collectionView.reloadData()
}
}

1 section and 2 cells but 4 indexPath.row

Swift 4.
I want to return two cells in one section, so in my class CollectionViewController I do :
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1 }
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2 }
I see two cells but if I print indexPath.row in the code below (still in the same class), I see 0 1 0 1. Why is it not only one 0 1as I have only two cells in one section ?
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
print(indexPath.row)
return cell
}
cellForItemAt is getting called four times. Your numberOfItemsInSection is hard-coded for 2. When the view loads cellForItemAt is called twice (once for each cell). It is then getting called twice again when you call reloadData() from your DispatchQueue.main.async closure.
Updated - How to avoid the first call:
You need to store your cell data in an array, and only populate the array just before you call reloadData(). Thus, the array will be empty when the view is first loaded.
var yourArray = [YourObject]() //Empty Array
//..
DispatchQueue.main.async {
//append your two items to the yourArray
yourArray.append(/*cell1data*/)
yourArray.append(/*cell2data*/)
self.collectionView?.reloadData()
}
//..
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return yourArray.count
}

UIcollectionView Cell set ZIndex . I want to create Card Cell using UICollectionView

Swift
I am trying to create card style cell. So i am using collection view and set minimumLineSpacingForSectionAt in negative this code is working it looks good.
Problem is when I have a scroller bottom to top then first cell is a top side but I don't have a need on top here i have attached my code please check. I have Attached Error Image after scrolling issue
class SecondViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
let kItemSpace: CGFloat = -60.0
#IBOutlet weak var collection_setting: UICollectionView!
#IBOutlet var btn_top: UIButton!
var ary: NSMutableArray = NSMutableArray()
var ary_show: NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
ary = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//-----------------------------------------------------------------------------------
// MARK: - collection DataSource & Delegate
//-----------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.ary.count
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "smallCollectionViewCell", for: indexPath as IndexPath) as! smallCollectionViewCell
cell.inner_view.dropShadow()
cell.inner_view.backgroundColor = .random()
cell.layer.zPosition = -1
cell.lbl_.text = "DEMO - \(indexPath.row)"
//cell.bringSubview(toFront: collectionView)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 100 )
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: NSInteger) -> CGFloat {
return kItemSpace
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print(indexPath.row)
if(indexPath.row == 14)
{
print("--------------->",indexPath.row)
cell.layer.zPosition = 0
}else
{
cell.layer.zPosition = -1
}
}
}
Demo - 7 ,Demo - 8 , Demo - 9 is looking good but others are not showing as I want . first time this is looking correctly. this problem has come after scrolling.
Try this code please.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "smallCollectionViewCell", for: indexPath as IndexPath) as! smallCollectionViewCell
if cell == nil {
cell.inner_view.dropShadow()
cell.inner_view.backgroundColor = .random()
}
cell.layer.zPosition = -1
cell.lbl_.text = "DEMO - \(indexPath.row)"
return cell
}
cell.layer.zPosition = CGFloat(array.count - indexPath.row)

Why does my UICollectionViewController not show anything?

I'm trying to make a UICollectionViewController show a grid of cells (in which I'll later add text, likely as textViews if possible). I have:
class GridViewController : UICollectionViewController{
//(NOW GONE) #IBOutlet weak var gridViewTable = UICollectionViewController()
let arr1 : [String] = []
let arr2 : [String] = []
override func viewDidLoad(){
super.viewDidLoad()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "gridCell")
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
return cell
}
}
I used the interface builder to connect white where the cells will go to the gridViewTable. Nothing shows up at all, even tho it runs and compiles. How can I get the cells to show?
please use these code:
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.red
return cell
}
You don't need the outlet for a UICollectionViewController, the class itself takes care of that.
Also, make sure that the view controller's class in Interface Builder is set to GridViewController in the Identity Inspector (3rd tab from the left in the right-most pane).