Xcode Swift UICollectionView: cellForItemAt IndexPath not called but numberOfItemsInSection does - swift

I am currently programming an app with UICollectionView only when I start the app the CollectionView is not displayed. In my app I do not use the StoryBoard but only the ViewController. That's why I inherit from UICollectionViewController in my app. But when I start the app, the UICollectionView is simply not displayed. Through print commands I found out that the numberOfItemsInSection method is called, but the cellForItemAt indexPath is not. I also know that this will probably be a double question, but I still haven't found a solution for 2 hours. CollectionView.delegateand collectionView.dataSource are both set to = self and there are actually no problems with the constrains because I can turn the background of the UICollectionView to black, for example, and everything is displayed correctly there.
I hope someone can help me.
Best regards
// MARK: - Collectionview
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("Item wurde erstellt")
return messages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCollectionViewCell
let message = messages[indexPath.item]
cell.textView.text = message.message
setUpCell(cell, message)
cell.bubbleViewWidthAnchor?.constant = estimateFrameForText(text: message.message!).width + 32
print("Celle wird erstellt")
return cell
}

I think you have missed the height and width for the cell. You should also check the message array count, perhaps it is zero.
extension YouClass: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCollectionViewCell
let message = messages[indexPath.item]
cell.textView.text = message.message
setUpCell(cell, message)
cell.bubbleViewWidthAnchor?.constant = estimateFrameForText(text: message.message!).width + 32
print("Celle wird erstellt")
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collBanner.frame.width, height: 140)
}
}

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.

How to make a line break in a label in Xib

I am trying to make a line break to my icon description, and I tried with /n and checking the multiple lines option in the interface builder.
Aqui es donde saco los datos de mi Icon
It should be noted that my icon is a CollectionViewCell that is generated inside a tableViewCell:
extension MiniAppsShorcuts : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.listAr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AccesosRapidos", for: indexPath) as? AccesosRapidos else {return UICollectionViewCell()}
cell.labelAR.text = listAr[indexPath.row].name
cell.fetchImage(urlString: listAr[indexPath.row].urlImage)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.alert()
}
}
Make sure to set your cell.labelAR numberOfLines to zero and lineBreakMode to byWordWrapping. The default value is byTruncatingTail. That’s why it is truncating your label. Make sure your label and your cell heights are large enough to have multiple lines.

UICollectionViewDataSource cellForItemAt don't run at iOS 14

The cellForItemAt dont't get called after I updated the build target to iOS 14 in Xcode. However, numberOfItemsInSection is getting called but cellForItemAt don't which makes it weird. I have never seen this before.
Any idea of what it could be?
extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.cells.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoginCollectionCell",
return cell
}
I can't see anywhere where you set the cell sizes so be sure your cells content sizes are being setup correctly. If you're using a flow layout you can set the size like this:
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 50, height: 50)
collectionView.collectionViewLayout = layout
Alternatively you can use the UICollectionViewDelegateFlowLayout protocol to return a size for the cell:
extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.cells.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoginCollectionCell",
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}

UICollectionViewLayout Not working with UIImage in Swift 5 and Xcode 11

Please check this attached code and screen short. It's work fine when I set the color of container view but when I add UIImage in cell then it's not working. I changed the imageview content mode but it's not working.
class ViewController: UIViewController {
#IBOutlet weak var segColumn: UISegmentedControl!
#IBOutlet weak var collectionView: UICollectionView!
fileprivate var items: [UIImage] = [ // My images ]
override func viewDidLoad() {
super.viewDidLoad()
self.setup()
}
}
extension ViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imgCell.image = items[indexPath.row]
cell.imgCell.contentMode = .scaleAspectFill
cell.contentView.backgroundColor = .red
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.bounds.width/3 - 2), height: (collectionView.bounds.width/3))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
}
Please try this solution. I think there are some changes from Xcode 11.
Change the CollectionView Estimate Size to None then it will work.
it is probably because the red is dominating over the image
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.imgCell.image = items[indexPath.row]
cell.imgCell.contentMode = .scaleAspectFill
cell.contentView.backgroundColor = .red
return cell
}
try:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.contentView.backgroundColor = .red
cell.imgCell.image = items[indexPath.row]
cell.imgCell.contentMode = .scaleAspectFill
return cell
}

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)