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
}
Related
I have UICollectionView and I add all the videos from my photo Library in an array and display them in the CollectionViewCells.
I want to be able select the videos from this UICollectionView.
How do I achieve this ? .Thanks
it's simple. you should use "didSelectItemAt indexPath" method:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
// and now you access your view that is in the cell
}
I have an odd problem with my UICollectionView. When I select the first cell in my Collection View the last cell is highlighted but does not show up in the array of selected index paths. Why is this happening?
Here is the code I am using to select and deselect the cells?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = highlightedCellBorderWidth
cell?.layer.borderColor = UIColor.yellow.cgColor
selectedImages.append(imageArray[indexPath.item])
print(selectedImages)
print(collectionView.indexPathsForSelectedItems)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 0
let index = selectedImages.index(of: imageArray[indexPath.item])
selectedImages.remove(at: index!)
print(selectedImages)
print(collectionView.indexPathsForSelectedItems)
}
You don't show how your cell is initialized, but...
collectionView(_:didDeselectItemAt:) is only called when the user successfully deselects an item. It does not get called if you deselect an item programmatically. If you are not resetting your layer's borderWidth when initializing a cell, the non-zero width will carry forward when the cell is reused by the collection view.
I found this link on S.O. which was helpful. To solve the problem I simply added the following code to my func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}.
Code...
if collectionView.indexPathsForSelectedItems?.contains(indexPath) == true {
print("This cell is selected \(indexPath.item)")
cell?.layer.borderWidth = 3
cell?.layer.borderColor = UIColor.yellow.cgColor
}else{
cell?.layer.borderWidth = 0
}
I have looked through google and stack overflow and I was not able to find an answer. I have a UICollectionView and would like to take the user to another view upon the cell being clicked. But before doing so I would like to click the cell on the simulator and have the label's name printed in the console so I can from there figure out how to write the performSegue method. I am having issues with the didSelectItemAt function.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let clothesCell = collectionView.dequeueReusableCell(withReuseIdentifier: "clothesCell", for: indexPath) as! closetCollectionViewCell
clothesCell.clothingName.text = shirtStyle[indexPath.item]
clothesCell.clothingColor.text = shirtColor[indexPath.item]
clothesCell.clothingSize.text = "\(sizes[indexPath.item])"
return clothesCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item.clothingName)
}
Use this for print in didselectItem for selected items
print(shirtStyle[indexPath.item])
You need to get the cell that was clicked at indexPath like this:
let cell = collectionView.cellForItem(at:indexPath) as! closetCollectionViewCell
Then just get the values from the cell variables and print.
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.
I am new to iOS developing and I am using Swift 3.
In my gallery app when user selects a cell, the picture inside that cell displays in a UIImageView above and the cell itself becomes bordered red to show that it is selected right now. However, when I scroll up or down and as soon as the selected cell is destroyed UICollectionView selects another cell from the visible ones.
I want to know how can I restore the selected state of a cell when it reused and prevent UICollectionView from doing that. In conclusion I want to know how to prevent from cell reusabilities effects on selection state.
Sorry for my bad English, not a native speaker.
you can either have an array of the images states if it's true then it's selected else it's not and in the
collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
function you check on the state and do the required setup for the cell or you can make the collection view don't reuse the cells by changing the cell identifier dynamically like this
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCircularCollectionViewCell\(indexPath.row)", for: indexPath) as? HomeCircularCollectionViewCell
//setup cell
return cell
}
hope my answer helps you in your problem