new to swift. I have a nested CollectionView from one viewcontroller. The main viewcontroller has 7 collectionviewcell ("Level1Cell" in the code below). Each time I click a button or trigger an event, I want the collectionView to reload with the new data.
func eventHandler() {
// updates data
myCollectionView.reloadData()
}
Then, after it calls reload, it will call the reload again on each of the the nested CollectionViewCell.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Level1Cell", for: indexPath) as! Level1Cell
cell.appsCollectionView.reloadData()
return cell
}
The problem is, let say I want to, for the first cell, set a particular row some text.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(self.index == 0 && indexPath.row == 30){
rightCell.textLabel.text = "asdasd"
}
The fourth "Level1Cell" cell somehow has its label set also at the 30th row, but not the second and third. After stepping through the debugger, I realize that the cells, after reloading, the fourth cell "Level1Cell" is set to have the the same memory address as the first cell ( why does reload do this - shouldn't it allocate a new memory for each "Level1Cell"? - how can I get around this). Also, should I not use reload to update the data in the view and nested view of those from the view controller?
Thanks!
UIcollectionview will reuse cells. You must provide needed data for row in method
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
Or just clear previous data at cell.
Related
I have come across a very disturbing issue in which I am not able to dequeue only a single cell in collectionView. When there are more than one item in collectionView, it works fine but soon as I have an array with a single item, it doesn't show any cell. I am not able to figure it out what’s wrong with my code.
extension collectionview: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewCell
return cell
}
}
The number returned for numberOfItemsInSection should be the total number of objects you want to show, so if you have an array of objects for example, you would want to return the count of that array. Since it's currently set to one, only one item will be shown. If you change that to match the number of items you're displaying, then the collectionView should cooperate.
First:
the returned of numberOfItemsInSectionwill be array.count for Example
var array = [model]()
Then in cellForItemAt you should configure your cell with data so
Add this block in your cell class
func configureCell(_ data: model) {
// code for cell view
}
Finally cellForItemAt will be
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewCell
cell.configureCell(array[indexPath.item])
return cell
}
I hope this be helpful for you.
Needless to say, collection views work fine with only one cell. Your code is insufficient to manifest the problem you describe.
But here are a few observations/suggestions:
Add breakpoint or debugging log statement in numberOfItemsInSection and make sure it’s getting called. If not, then you likely have one of a variety of different possible problems:
Make sure you set the “data source” for the collection view (either in IB or programmatically). Merely conforming to UICollectionViewDataSource is not sufficient: You actually have to set the data source of the collection view.
Make sure you set the base class for your storyboard scene to be the view controller in question.
Make sure you put these UICollectionViewDataSource methods in the right object.
I notice that you've put these methods as an extension to collectionview.
Class names should, as a matter of convention, always start with upper case letter.
Usually the UICollectionViewDataSource methods are put in the view controller (or, less common, a dedicated object that you'll vend and to which keep your own strong reference). I'm not sure what to make of your class named collectionview.
But, in short, there is no problem having collection views with only one cell. Your problem rests elsewhere.
I have more than one collectionView in a ViewController. The cell of those collectionViews has the same format.. so I'm reusing them. So my question is: How to identify in the method
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath)
I don't want to do a couple of if's
I've found this solution everywhere, but really don't like it. Here is the code
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
if let aCell = cell as? ItemCollectionViewCell{
aCell.setupCell(with: self.items[indexPath.item])
}
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.colletionViewTwo{
// goto viewController1
}else if collectionView == self.colletionViewOne{
// goto viewController2
}
}
Create two classes that implement the collection view delegate and data source and use one of each. So you'll have these two extra objects in your current view controller.
Seeing your code now, the above is probably too heavy. Alternatively, add a dictionary in which you store the collection view as key and a selector as value. This is extensible as you say you want.
To be honest, what's your issue an if (or switch) statement like you have now?
I am creating a project where I have a lot of options for user to select among different plans.
So I created a UITableViewCell, put UICollectionView inside with one UICollectionViewCell as one plan.
Now user can select only one plan out of all.
Let's say the user selects the second plan, I need to update the price on a button which is in UITableViewCell.
I know we can reload tableCell and update. But is there any other way to do except reloading UITableViewCell
Let me know the best way to do that except reloading whole tableviewcell
In didselect method of Uicollectionview, get the tableviewcell of price displaying and update the button.
CollectionView didSelectItem method
{
NSIndexPath *indexpathoftableviewcell = [NSIndexPath indexpathForRow:1 inSection:0];
UITableViewCell *priceCell = [tableView cellForRowAtIndexPath: indexpathoftableviewcell];
UIButton *priceBtn = (UIButton*)[priceCell viewWithTag:tagOfBtn];
[priceBtn setTitle:#"43432" forControlState:UIControlStateNormal];
}
Assuming your UITableViewCell loads the UICollectionView inside it, so the obvious choice for UICollectionView's DataSource and Delegate should be your UICollectionViewCell.
So in your TableView Cell you can simply access the label that you need to update with price as shown below
extension MyTableViewCell : UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let someCell : MyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "your_identifier", for: indexPath) as! MyCollectionViewCell
return someCell
}
}
extension MyTableViewCell : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! MyCollectionViewCell
self.priceLabel.text = "\(cell.value)"
}
}
here I am assuming that your CollectionViewCell is a custom cell and has a property named value where it store the price of plan
Accommodate the Plans (UICollectionView) & Price (UILabel or UIButton) in the UITableViewCell rather than making it as separate cells
Here is the reason behind it.
Both Price and Plans are tightly coupled. It means, When you change plan the respective price should come and this requirement may grow in future like adding some more views. If you accommodate the entire part in one UITableViewCell, it will become very easy. This way you can manage your design future proof.
regarding the question, the price is just like one more IBOutlet in the CustomTableViewCell. Just update the price in
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {}
What is the most common way to delete cells from a UICollectionView?
In a UITableView I use the editActionsForRowAt or editingStyle methods to delete rows, does UIControllerView has something similar or you need to implement your own deleting method?
What I have is a UICollectionView with a lot of photos where each cell/photo is attached to a segue which takes you to a larger version of the photo.
The easiest way I could be to do it in the didSelectItemAt method but in my case that is not an option since as soon as a photo is tapped it segues to the other viewController (larger image).
What would be the best way to add a deleting functionality in a situation like the one I'm describing above?
The following threads show how to delete using the didSelectItemAt.
How to add a delete button to Collection View Cell in Swift?
How to delete item from collection view?
CODE
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return fruits.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fruitCustomCell", for: indexPath) as! fruitCollectionViewCell
cell.labelFruitName.text = fruits[indexPath.row].fruitName
return cell
}
App Diagram
I have put UITableView in UICollectionView, and it has 4 items, but when I scroll UICollectionView to the last item, it shows the content of the first UITableView ( So when I press option 3 button it scrolls to the last item of collectionView but the content is wrong ) . You can have a look at the below picture.
Below is the link to my project, please help me out with this issue.
Link to my project
You have to reload the UITableView in cellForItemAt as mentioned below.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TableCollectionCell", for: indexPath) as! TableCollectionCell
cell.tableView.tag = indexPath.item
cell.tableView.reloadData()
return cell
}