How to make a line break in a label in Xib - swift

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.

Related

Xcode Swift UICollectionView: cellForItemAt IndexPath not called but numberOfItemsInSection does

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)
}
}

collectionView cellForItemAtIndexPath called only one time -swift - programmatically

I have a collectionView that I set with this delegate and datasource:
extension CardView : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let numberOfURLS = cardModel?.urlStrings?.count
return numberOfURLS!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let videoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCellIdentifier", for: indexPath)
videoCell.backgroundColor = UIColor.random()
return videoCell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return self.bounds.size
}
}
The problem is that the method cellForItemAtIndexpath is called only one time even if the cells returned are more than one.
The collectionView(_:cellForItemAt:) is called only as the cells appear or are about to appear. It won't call it for all of the cells, but only the visible ones (or those about to scroll into view). It is called in a just-in-time manner.
You can also turn on prefetching (which only works if you turn off “Estimate Size” in the collection view’s “Size inspector” IB or manually set the flow layout’s estimatedItemSize to .zero), but that only makes it a tiny bit more “eager” in terms of fetching cells (fetching those that are going to scroll into view a bit sooner than it would otherwise). It will not fetch all of the cells, but just those that the OS determines might possibly scroll into view soon.

didDeselectItemAt For UIcollectionView is not called but didSelectItemAt is called?

I am trying to implement a multi selection for UICollectionView. The delegate function didSelectItemAt is called but didDeselectItemAt is NEVER called and I do not know why? I am not even sure how it works. If I click on a cell - didSelectItemAt is called. So if I click once again on the same cell is didDeselectItemAt supposed to be called?
My UIViewController inherits and conforms to all of the following: UICollectionViewDelegate ,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
viewdidLoad():
collectionView.allowsSelection = true
collectionView.allowsMultipleSelection = true
Delegate functions:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}
Please see this open issue on Github.
It could be that you have a tap gesture recognizer on a view in the same hierarchy that contains your collection view. Remove the tap gesture and see if that works. This happened to be my issue.
let tap = UITapGestureRecognizer(...) // your tap gesture recognizer
view.addGestureRecognizer(tap) // what you already have
tap.cancelsTouchesInView = false
I'm going to guess that your code is different from what you have shown us, and in that in your real code, you have not given the correct signature for didDeselect. Here's why. Look carefully at the code you have shown:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}
Now ask yourself: Why did the compiler permit the second func to stand, even though you forgot to say override? I'm guessing it's because it is not an override. There is something wrong with the signature, so it's just a meaningless function that doesn't conform to UICollectionViewDelegate.
Try using code completion to re-enter this function. If all goes well, it will be an override and it will start working.
To illustrate more precisely: This compiles, but the second method will never be called:
class CV : UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")
}
func colectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}
}
But this doesn't compile, because the signature is correct but we forgot override:
class CV : UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}
}
But in that second one, if we do say override, it compiles and works.
It works like this:
if collectionView.allowsMultipleSelection = false // default then you have to tap on a different cell in order to deselect the previous one.
if collectionView.allowsMultipleSelection = true // only tapping on the same cell triggers deselect delegate method
I'm not sure if this changed recently, but with iOS13 it seems that simply specifying that a collection view cell is selected when the cell is dequeued is not enough for the collection view to allow you to deselect the cell. You need to also manually tell the collection view to select the cell.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoCellIdentifier, for: indexPath) as! YourCell
cell.isSelected = true
if cell.isSelected {
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .left)
}
}
I figured this out because i was able to select and deselect cells, but was not able to deselect a cell that was initially selected.

UICollectionView Cell not changing upon selecting

In my project, I have a UICollectionView. In the UICollectionView, I have a custom cell.
I am able to print the cell value when it is selected within "didSelectItemAt", however, if I try to edit the cell in any way within this method, it does not change.
I'm sure I'm missing something, any help would be appreciated!
#IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return statValues.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCollectionViewCell", for: indexPath) as! customCollectionViewCell
cell.statLabel.text = statHeaders[indexPath.row]
cell.statLabel.textColor = UIColor(red:0.31, green:0.31, blue:0.31, alpha:1.0)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCollectionViewCell", for: indexPath) as! customCollectionViewCell
print(cell.statLabel.text)
cell.backgroundColor = UIColor.yellow
collectionView.reloadData()
}
When user selects a cell, the code is correctly printing the value of the indexPath, however the backgroundColor does not change.
My guess would be that you are creating a new instance of cell instead of using the one in the collectionView
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Change this line
let cell = collectionView.cellForItemAtIndexPath(indexPath: indexPath)
print(cell.statLabel.text)
cell.backgroundColor = UIColor.yellow
collectionView.reloadData()
}
Also, you should probably keep an external data model for your source of truth. If you have enough collectionViews that requires scrolling, when you scroll offscreen, your cells will be reused in a random order causing cells that you did not click to be yellow.
Create a seperate array such as
var selectedStatHeaders: Set<Int>()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCollectionViewCell", for: indexPath) as! customCollectionViewCell
cell.statLabel.text = statHeaders[indexPath.row]
cell.statLabel.textColor = UIColor(red:0.31, green:0.31, blue:0.31, alpha:1.0)
// Reset/configure cell each reload
if selectedStatHeaders.contains(indexPath.row) { // Can also make this into a ternary
cell.backgroundColor = UIColor.yellow
} else {
cell.backgroundColor = UIColor.whit
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedStatHeaders.insert(indexPath.row)
collectionView.reloadItemsAtIndexPath(indexPath: indexpath)
}
Hmm... Doesn't make sense if the code is able to print but the background doesn't change color. Do you mean changing back from yellow to white? Anyway, just a hunch but I suspect it's because you are calling collectionView.reloadData() after your set the backgroundColor change.
https://developer.apple.com/documentation/uikit/uicollectionview/1618078-reloaddata
This causes the collection view to discard any currently visible
items (including placeholders) and recreate items based on the current
state of the data source object.

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)