For some reason, my collectionViewCell's are unrecognized when they are selected. It's not until another cell is touched afterwards that the previous cell is recognized. To explain how I realized this, I added the following code to my collectionView's didDeselectItemAtIndexPath method: println("user tapped on cell # \(indexPath.row)"). When I run the app and select a cell, my console doesn't respond until I tap another cell, then it reads the println I added. For instance, if I select the first cell, the debugger doesn't print anything until i select another cell, then the console reads "user tapped on thumbnail # 0".
Now I've added an animation to my collectionView that enlarges each cell on selection, so this is how I know it isn't an indexPath issue because the cell indexPath # that is printed in the console is the correct cell that is enlarged in the view, but like i said, the cell isn't animated on its selection, not until i select another cell afterwards.
Here is my collectionView delegate and dataSource logic:
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.books.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
// Configure the cell
let book = self.books[indexPath.row]
let coverImage = book.coverImage
if coverImage == nil {
book.fetchCoverImage({ (image, error) -> Void in
if self.collectionView != nil {
collectionView.reloadItemsAtIndexPaths([indexPath])
}
})
} else {
let imageView = cell.imageView
imageView.image = book.coverImage
}
return cell
}
override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
let book = self.books[indexPath.row]
self.selectedImageView = cell.imageView
if !isModeModal {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController
controller.imageSelected = book.coverImage
self.navigationController?.pushViewController(controller, animated: true)
}
println("user tapped on thumbnail # \(indexPath.row)")
}
}
Why is this behavior occurring?
I didn't run your code, but I think, that the problem could be caused by didDeselectItemAtIndexPath. Try to use didSelectItemAtIndexPath instead.
If necessary you can add deselectItemAtIndexPath :
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
collectionView.deselectItemAtIndexPath(indexPath: NSIndexPath, animated: Bool)
}
Related
I am using a UICollectionView inside UITableViewCell. I am able to select the cells inside the UICollectionView. But when i try to get the UICollectionView or selected cells, the result is always null.I have been stuck on this for a long time. i included my code below for your reference.
class WeekDaysSelCell: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var weekdays = ["S", "M", "T", "W", "T", "F", "S"]
var weekdaysSelected = [String]()
#IBOutlet var weeklyDaysColView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
self.weeklyDaysColView.delegate = self
self.weeklyDaysColView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : WeekDaysCollCell = weeklyDaysColView.dequeueReusableCell(withReuseIdentifier: "weekday", for: indexPath) as! WeekDaysCollCell
cell.weekDayLabel.text = weekdays[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell : WeekDaysCollCell = self.weeklyDaysColView.cellForItem(at: indexPath) as! WeekDaysCollCell
if (cell.backgroundColor == UIColor.gray) {
cell.backgroundColor = UIColor.clear
weekdaysSelected.removeAll { $0 == String(indexPath.row)}
//print("Removed from weekdaysSelected:", indexPath.row)
} else {
cell.backgroundColor = UIColor.gray
cell.isSelected = true
//weeklyDaysColView.selectItem(at: indexPath, animated: true, scrollPosition: [])
weekdaysSelected.append(String(indexPath.row))
//print("Added to weekdaysSelected:", indexPath.row)
}
}
}
// Trying to get the collection view from inside a willMove(toParent parent: UIViewController?) method.
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent: parent)
if parent == nil
{
if let delegate = self.delegate {
print("Inside If condition")
// Code that i use to get the cell
let cell3 = tableView.dequeueReusableCell(withIdentifier: "cell3") as! WeekDaysSelCell
print(cell3.weekdaysSelected)
print(cell3.weeklyDaysColView.indexPathsForSelectedItems)
// Trying to pass selected cells
//delegate.repeatCustomSelection(selectedIdx: String(lastSelection.row),repeatCustomSel: repeatCustomSelection)
}
}
}
You are trying to get a reusable cell in willMove(toParent parent: UIViewController?) , this is not going to return you a expected cell.
You need to get the cell , using a indexPath .
func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
#andyPaul, is right you are generating the new cell in willMove(toParent parent: UIViewController?). Instead of that you have to pass the indexpath pf collection view when ever user selected any cell to your controller from the tableView Cell Class.
Now What is TypeAlias you can read from this link about type alias:- https://www.programiz.com/swift-programming/typealias
Create the typeAlias on above of your tableViewCell Class like this:-
typealias closureBlock = (_ isCapture : AnyObject?) ->()
class tableViewCellClass: UITableViewCell {
var callBack: closureBlock?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
Just Go to CollectionView didSelectItemAt Method and use this code after your coding
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell : WeekDaysCollCell = self.weeklyDaysColView.cellForItem(at: indexPath) as! WeekDaysCollCell
if (cell.backgroundColor == UIColor.gray) {
cell.backgroundColor = UIColor.clear
weekdaysSelected.removeAll { $0 == String(indexPath.row)}
//print("Removed from weekdaysSelected:", indexPath.row)
} else {
cell.backgroundColor = UIColor.gray
cell.isSelected = true
//weeklyDaysColView.selectItem(at: indexPath, animated: true, scrollPosition: [])
weekdaysSelected.append(String(indexPath.row))
//print("Added to weekdaysSelected:", indexPath.row)
}
guard let callBackClosure = self.callBack else {
return
}
callBackClosure(indexPath as AnyObject)
// You can pass any value here either indexpath or Array.
}
}
Now you have to initialise this closure so that it can check whether in which controller it will return the value when you assign the value from CollectionView didSelectItemAt Method.
Go to your ViewController Class where you have added the tableview and their datasources.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//You have to pass your tableview Cell Instance here and their reuse Identifier
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCellClass", for: indexPath) as! tableViewCellClass
cell.callBack = { [weak self] (selectedIndexPath) -> ()in
// You will get the current selected index path of collection view here, Whenever you pass any index path from collectionView did SelectItem Method.
print(selectedIndexPath)
}
return cell
}
I am trying on my viewDidLoad() to fetch images from my DB and then present the images, and highlight the first one.
My code thus far is:
profile?.fetchImages(onComplete: { (errors, images) in
for error in errors {
print("Error", error)
}
for imageMap in images {
self.photos.append(imageMap.value)
}
if self.photos.count < self.MAX_PHOTOS {
self.photos.append(self.EMPTY_IMAGE)
}
DispatchQueue.main.async {
self.photoCollectionView.reloadData()
}
self.updateSlideshowImages()
let indexPath = IndexPath(row: 0, section: 0)
let cell = self.photoCollectionView.cellForItem(at: indexPath)
if cell != nil {
self.setBorder(cell!)
}
})
However, for me cell is always nil, despite images existing and being fetched, and thus the setBorder is never called. Why is the cell always nil? I just want to set the border on the first cell.
You have placed self.photoCollectionView.reloadData() in async block so the let cell = self.photoCollectionView.cellForItem(at: indexPath) will run immediately before collection view reload, thats the reason you are getting nil for first cell.
You need to make sure that after collection view reload, I mean when all collection view cells are loaded then you can get and do your operations.
Alternatively, you can do this in cellForItemAtIndexPath like below...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier.jobCollectionViewCellIdentifier, for: indexPath) as! <#Your UICollectionViewCell#>
if indexPath.section == 0 && indexPath.row == 0 {
//highlight cell here
}
return cell
}
And if you want to highlight cell after all images load in collection view then you need to check for datasource of collection view.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ...
if indexPath.row == arrImages.count-1 {
let newIndexPath = IndexPath(row: 0, section: 0)
if let cellImg = self.photoCollectionView.cellForItem(at: newIndexPath) {
self.setBorder(cellImg)
}
}
}
Instead of highlighting the UICollectionViewCell in viewDidLoad() after receiving the response, highlight it in willDisplay delegate method, i.e.
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)
{
if indexPath.row == 0
{
cell.isHighlighted = true
}
}
And add border of cell in the isHighlighted property of UICollectionViewCell according to the cell highlight status, i.e.
class CustomCell: UICollectionViewCell
{
override var isHighlighted: Bool{
didSet{
self.layer.borderWidth = self.isHighlighted ? 2.0 : 0.0
}
}
}
You can use isSelected property the same way in case you want to change appearance of the cell based on the selection status.
Let me know if you still face any issue.
Set your cell's border in cellForRow method.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath) as! YourCustomCell
if indexPath.row == 0 {
self.setBorder(cell)
}
return cell
}
I want to select all and deselect all cell of collectionView using select All and deselect all UIButton?
Please help anyone.
//Delegate Method cellForItemAtIndexPath
func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell
{
//Get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
"pickSomecell",
forIndexPath: indexPath) as! pickSomeGridViewController
//Show Images in grid view
cell.cellImage.image = self.arrAllOriginalImages[indexPath.row] as? UIImage
cell.toggleSelected()
//return cell.
return cell
}
Thanks
try to do as follow-
var isSelectAll=false
func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell
{
//Get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(
"pickSomecell",
forIndexPath: indexPath)
//Show Images in grid view
if isSelectAll {
cell.cellImage.image = selectedImage;
}else{
cell.cellImage.image = desselectedImage;
}
return cell
}
#IBAction func selectAllcell(sender: AnyObject) {
isSelectAll=true
collectionView.reloadData()
}
#IBAction func deselectAllcell(sender: AnyObject) {
isSelectAll=false
collectionView.reloadData()
}
So first of all i've been stuck on this for a few days and spent a full day reading and trying many options on Stack Overflow already but non to my success
What i'm trying to accomplish sounds simple and going over the Apple documentation it seems to me it should work
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegate_protocol/#//apple_ref/occ/intfm/UICollectionViewDelegate/collectionView:shouldHighlightItemAtIndexPath:
Basically what i'm trying to achieve is to toggle the selected state of a UICollectionView Cell on tap.
First tap - Send the cell into a selected state and change background colour to white.
Second tap - Send the cell into a deselected state and change background colour to clear
ViewController -
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as? CollectionViewCell {
cell.cellImage.image = UIImage(named: images[indexPath.row])
return cell
} else {
return CollectionViewCell()
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell {
cell.toggleSelectedState()
}
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell {
cell.toggleSelectedState()
}
}
Cell -
func toggleSelectedState() {
if selected {
print("Selected")
backgroundColor = UIColor.whiteColor()
} else {
backgroundColor = UIColor.clearColor()
print("Deselected")
}
}
The problem i'm having is the didDeselectItemAtIndexPath is not being called when tapping on a cell thats already selected, Though if i tap another cell it will get called and selects the new cell...
I have tried checking for selected states in shouldSelectItemAtIndexPath & shouldDeselectItemAtIndexPath, i even tried writing a tapGesture to get around this and still no luck...
Is there something i'm missing?
Or is there any known work arounds to this?
Any help would be greatly appreciated!
Use the shouldSelectItemAt and check the indexPathsForSelectedItems property of the collection view to determine if the cell should be selected or deselected.
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
if let indexPaths = collectionView.indexPathsForSelectedItems, indexPaths.contains(indexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
return false
}
return true
}
Maybe you can create a UIButton() with the same bounds as the cell, and identify the select in the button. Then in the tap action of the button, you can do something to 'disselect' the cell 'selected'.
You may set the UICollectionView's allowsMultipleSelection property to YES(true), then the collection view will not deselect the previous item.
I made this work for a collection view that allows multiple cells to be selected and deselected by using a tap gesture recognizer.
Cell:
class ToggleCollectionViewCell: UICollectionViewCell {
var didChangeSelection: (Bool) -> Void = { _ in }
required init?(coder: NSCoder) {
super.init(coder: coder)
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap))
addGestureRecognizer(tap)
}
#objc private func didTap() {
isSelected.toggle()
didChangeSelection(isSelected)
}
}
Controller:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "toggleCell", for: indexPath)
if let toggleCell = cell as? ToggleCollectionViewCell {
// do your cell setup...
lineCell.didChangeSelection = { isSelected in
guard let self = self else { return }
// ... handle selection/deselection
}
}
return cell
}
I have a problem with a simple collection view: in the delegate didSelectItemAtIndexPath I can't get the right reference to the cell. This is my code:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return categories.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CategorySelectionCVCell
cell.titleLabel.text = categories[indexPath.row]["name"] as? String
cell.backgroundColor = ConversionUtilities.getUIColorFrom(categories[indexPath.row]["color"] as! String)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CategorySelectionCVCell
print("Cell with title: \(cell.titleLabel.text) and background color: \(cell.backgroundColor)")
collectionView.reloadData()
}
I get always when I select a cell:
Cell with title: Optional("Label") and background color: nil
and I don't know why, it's like the cellForItemAtIndexPath is not working but the same print in cellForItemAtIndexPath give me:
Cell with title: Optional("fruits") and background color:
Optional(UIDeviceRGBColorSpace 1 1 0 1)
...and yes, the cell identifier is right is "cell" and I also set up correctly in storyboard. In addition UICollectionViewDataSource and UICollectionViewDelegate are also correctly linked in IB.
Any help?
When you call dequeueReusableCellWithReuseIdentifier method, i.e.
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CategorySelectionCVCell
you get an empty cell ready to be reused, not the cell that is currently in your collection view.
Although you can get the cell from the collection view by calling
let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! CategorySelectionCVCell
if all you need is the text from its label it's better to go straight to the model, i.e.
let labelText = categories[indexPath.row]["name"] as? String