UIcollectionview Custom Cell class to didselectitem at - ios

my collectionview contains multiple custom cell class that created programmatically. Fun part is I will have a cell that contains another collectionview inside of it. So how to click into this collectionview to present a new view controller? Below are my sample codes on how to create the cell.
class MainView: UIViewController {
** main view to display UIcollectionview **
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! UserContainerViewCell
cell.Users = users * to display users array*
return cell
}
class UserContainerViewCell: UICollectionViewCell{
* the uicollectionview that inside a cell , also contains extension to UIcollectioview datasource and delegate to display the cells within this cell*
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ident, for: indexPath) as! userCell
let user = featureUsers![indexPath.row]
cell.config(withUser: user)
return cell
}
}
class userCell: UICollectionViewCell {
func config(withUser: UserProfile) { }
** cell class to display the info from the user array **
}
So with this setup, how to click into the cell and present a new controller with the User info in the array?

You have to pass a delegate or hit notification to viewController on didSelect of internal collectionViewCell and in that method you can present new view controller .
check out the link
Swift: How to use "didSelectItemAtIndexPath" in UITableViewController(contains UICollectionView)?

You can try
class UserContainerViewCell: UICollectionViewCell {
var myInstance: MainView!
}
class MainView: UIViewController {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! UserContainerViewCell
cell.myInstance = self
}
}
then inside didSelectItemAtRow
let vc = myInstance.storyboard?.instantiateViewController(withIdentifier: "infoView") as! InfoController
vc.providesPresentationContextTransitionStyle = true;
vc.definesPresentationContext = true;
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myInstance.present(vc, animated: false, completion: nil)

Related

Present ViewController by Clicking CollectionViewCell inside TableView(with several sections)

I'm making a music app recently, and I'd like to know how to pass data from CollectionView to TableView which has several sections. Here is the home page of my project, and what I want to do is when user tap the image, it will precent another ViewController with the information about the picture. I already know how to present a ViewController by clicking CollectionViewCell inside TableView by delegate, but only if there's only one section.
The thing is that I have 5 sections in this page, and I also have 5 different models for encoding the JSON from API. So how I show the pictures is to send the image urls(from 5 models) to TableViewCell in each section like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: HomeTableViewCell.identifier, for: indexPath) as? HomeTableViewCell else { return UITableViewCell() }
cell.delegate = self
switch homeSections[indexPath.section] {
case .newReleases:
if let newReleases = self.newReleases?.albums.items.map({$0.images[0].url}) {
cell.getPictures = newReleases
}
return cell
case .followSingers:
if let currentlyFollowing = self.currentlyFollowing?.artists.items.map({$0.images[0].url}) {
cell.getPictures = currentlyFollowing
cell.isCircle = true
}
return cell
case .catrgories:
if let categories = self.musicCategory?.playlists.items.map({$0.images[0].url}) {
cell.getPictures = categories
}
return cell
case .artists:
if let playlist = self.relatedArtists?.artists.map({$0.images[0].url}){
cell.getPictures = playlist
}
return cell
case .recentlyPlayed:
if let recentlyPlayed = self.recentlyPlayed?.items?.map({$0.track.album.images[0].url}) {
cell.getPictures = recentlyPlayed
}
return cell
}
}
However, when I want to pass the information which the user tap, there's nothing I can pass but indexPath. I've tried to declare the 5 different model types in TableViewCell, but I still don't know which section did the user tap. Does anyone can help? Thanks a lot!
Update:
To make the question more clearly, please refer to the information below.
In this page, I have a TableView and embed a CollectionView in the TableViewCell, and there's only one ImageView in the CollectionViewCell.
The "New Releases", "Currently Following" and "Categories" are the header of TableView. The "New Releases" is the first section, and the "Currently Following" is the second section, and so on.
Here is how I set cellForItem in CollectionView Delegate. It basically converts String to URL, and display the picture on the screen.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomeCollectionViewCell.identifier, for: indexPath) as? HomeCollectionViewCell else { return UICollectionViewCell() }
guard let url = URL(string: getPictures[indexPath.row]) else { return UICollectionViewCell() }
cell.myImageView.getImages(url: url)
if isCircle == true {
cell.myImageView.layer.cornerRadius = cell.myImageView.frame.width/2
}
return cell
}
When the user taps the image, it will only trigger the didSelectItemAt in CollectionView. And I can only pass indexPath.row so far.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.sendIndexPath(index: indexPath.row)
}

How to pass a collection view cell's indexPath to another view controller

I'm following a tutorial for making an Instagram-esque app, and the tutorial goes through how to display all data (image, author, likes, etc) all in one collection view. I'm trying to do it a little bit differently so only the images are displayed in the collection view, then if an image is tapped, the user is taken to a different view controller where the image plus all the other data is displayed.
So in my view controller with the collection view (FeedViewController), I have my array of posts declared outside the class (Post is the object with all the aforementioned data):
var posts = [Post]()
Then inside the FeedViewController class, my cellForItemAt indexPath looks like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell
// creating the cell
cell.postImage.downloadImage(from: posts[indexPath.row].pathToImage)
photoDetailController.authorNameLabel.text = posts[indexPath.row].author
photoDetailController.likeLabel.text = "\(posts[indexPath.row].likes!) Likes"
photoDetailController.photoDetailImageView.downloadImage(from: posts[indexPath.row].pathToImage)
return cell
}
And then I also obviously have a function to fetch the data which I can post if necessary, but I think the problem is because PhotoDetailController doesn't know the indexPath (although I may be wrong).
When I run the app and try to view the FeedViewController I get a crash fatal error: unexpectedly found nil while unwrapping an Optional value, highlighting photoDetailController.authorNameLabel line.
Am I correct in assuming the problem is because the indexPath is available only in the collection view data sources within FeedViewController? If so, how can I pass that indexPath to PhotoDetailController so my code works correctly?
Thanks for any advice!
EDIT: Edited my didSelectItem method:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let photoDetailController = self.storyboard?.instantiateViewController(withIdentifier: "photoDetail") as! PhotoDetailController
photoDetailController.selectedPost = posts[indexPath.row]
self.navigationController?.pushViewController(photoDetailController, animated: true)
}
and cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell
cell.postImage.downloadImage(from: posts[indexPath.row].pathToImage)
return cell
}
And in PhotoDetailController:
var selectedPost: Post! inside the class, then:
override func viewDidLoad() {
super.viewDidLoad()
self.authorNameLabel.text = selectedPost[indexPath.row].author
self.likeLabel.text = "\(selectedPost[indexPath.row].likes!) Likes"
self.photoDetailImageView.downloadImage(from: selectedPost[indexPath.row].pathToImage)
}
But still getting error use of unresolved identifier "indexPath
EDIT 2: Storyboard
You are getting this nil crash because this photoDetailController is not yet loaded so all its outlet are nil also the way currently you are doing is also wrong.
You need to add the controller code in didSelectItemAt method and perform the navigation.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let photoDetailController = self.storyboard?.instantiateViewController(withIdentifier: "YourIdentifier") as! PhotoDetailController
photoDetailController.selectedPost = posts[indexPath.row]
self.navigationController?.pushViewController(photoDetailController, animated: true)
}
Now simply create one instance property of type Post with name selectedPost in PhotoDetailController and set all the detail from this selectedPost object in viewDidLoad of PhotoDetailController.
Edit: Set your viewDidLoad like this in PhotoDetailController
override func viewDidLoad() {
super.viewDidLoad()
self.authorNameLabel.text = selectedPost.author
self.likeLabel.text = "\(selectedPost.likes!) Likes"
self.photoDetailImageView.downloadImage(from: selectedPost.pathToImage)
}

Access nested collectionView in viewController that does not have reference to it

I need to get reference of a collectionView that is nested inside another collectionView so that i can perform UI updates.
In my ViewController class i have an outlet for the "outer" collectionView:
#IBOutlet var outerCollectionView: UICollectionView!
I set the DataSource and Delegate for the "inner" collectionView in willDisplayCell:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let outerViewCell = cell as? OuterCollectionCell else { return }
outerViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forItem: indexPath.item)
outerViewCell.innerCollectionView.reloadData()
outerViewCell.innerCollectionView.layoutIfNeeded()
}
Then i can populate the cells for both collectionViews in cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.outerCollectionView {
let outerCell: OuterCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierOuter, for: indexPath) as! OuterCollectionCell
outerCell.labelCell.text = self.packArray[indexPath.item].title
// other stuff....
return outerCell
} else {// innerCollectionView
let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell
innerCell.imageCell.file = self.multiPartArray[collectionView.tag][indexPath.item].image
// other stuff....
return innerCell
}
}
This produces the following result:
Where collectionView.tag is the instance of the "inner" collectionView (0 being blue ect).
Now i want to perform UI updates on cells and for various reasons can not use cellForItemAt. So what i have done previously is something like:
if let editCell = collectionView.cellForItem(at: indexPath) as? InnerCollectionCell {
editCell.imageCell.image = UIImage(named: "test")
}
However to do this i need reference to the "inner" collectionView which i don't have because it may have been reused by the time these UI updates are performed. What i do have however is the collectionView.tag which is just an Int reference and the indexPath of the cell i want to perform the update on.
Can I the UI of the specified cell inside a collectionView that i don't have a direct reference to or is this just wishful thinking?
I should with some sort of delegate function i just cant think how. Also for reference below is my OuterCollection class where i define the "inner" collectionView and set the datasource and delegate.
class OuterCollectionCell: UICollectionViewCell {
#IBOutlet var labelCell: UILabel!
// define the smaller collection view that is in the cell
#IBOutlet var innerCollectionView: UICollectionView!
// set delegate and datasource for new collectionView
// this is download collection manager
func setCollectionViewDataSourceDelegate
<D: UICollectionViewDataSource & UICollectionViewDelegate>
(dataSourceDelegate: D, forItem item: Int) {
innerCollectionView.delegate = dataSourceDelegate
innerCollectionView.dataSource = dataSourceDelegate
innerCollectionView.tag = item
innerCollectionView.reloadData()
innerCollectionView.layoutIfNeeded()
}
}
you can have the delegate and datasource methods of innercollectionview defined in the OuterCollectionCell class. Try this:-
// Set the delegate and datasource in your custom cell class as below:-
class OuterCollectionCell: UITableViewCell {
// set the delegate & datasource of innercollectionView in the init or
// awakefromnib or storyboard.
// I have set it in storyboard and it works for me.
// Implement the datasource and delegate methods of innercollectionView in this class.
}
This way the delegate and datasource of innerCollectionView will be separate from the outercollectionview and you can handle all the logic of innecrCollectionView in the OuterCollection cell class.

cell variable is nil where value was set collectionview index path

i spend lot of time .. but i can't figure out why it is not working .. cell for index path called and set value properly but cell class i found nil value .. if your need any information then let me know .
in my collectionview index path
in collectionview cell
here is my code :
for collectionview :
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: menuBarItemDetailId, for: indexPath) as! MenuBarItemDetail
cell.shopCategory = "600"
print(cell.shopCategory)
return cell
}
for cell :
class MenuBarItemDetail: UICollectionViewCell , UITableViewDataSource , UITableViewDelegate {
var shopCategory : String?
override init(frame: CGRect) {
super.init(frame: frame)
print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
}
Because your awakeFromNib method called first and then cellForRow .You are assigning the value of variable in cellForRow so when first project executes its value is nil.
Solution
your variable in your custom cellclass
var myVar : String?
Create a method in your custom cellclass
func myMethod(str : String){
myVar = str
print("var : \(myVar)")
}
in cellForItem, call your function like this
cell.myMethod(str: "343")
Output
When you are writing
let cell = collectionView.dequeueReusableCell(withReuseId`entifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail`
At that time "override init(frame: CGRect)" is called.
and no value is assigned to shopCategory at the time of init that's the reason you are getting nil.
Add a function "getShopCategory" into MenuBarItemDetail and when every you want to access the value of shopCategory you can get that using getShopCategory function.
import Foundation
import UIKit
class MenuBarItemDetail: UICollectionViewCell {
var shopCategory : String?
func getShopCategory() {
print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
}
}
Controller Class
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail
cell.shopCategory = "600"
print(cell.shopCategory)
cell.getShopCategory()
return cell
}
cell is the current instance of MenuBarItemDetail so it will return assigned value of shopCategory
Please let me know if it's working for you.
Thank you

How to update the button tag which is part of UICollectionViewCell after a cell is deleted in UICollectionView?

Here's a problem which I have been stuck at for quite some time now.
Here's the code
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
collectionViewLove?.performBatchUpdates({() -> Void in
self.collectionViewLove?.deleteItemsAtIndexPaths([indexPath])
self.wishlist?.results.removeAtIndex(indexPath.row)
self.collectionViewLove?.reloadData()}, completion: nil)}
I have a button inside each UICollectionViewCell which deletes it on clicking. The only way for me to retrieve the indexPath is through the button tag. I have initialized the button tag in
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
However every time I delete, the first time it deletes the corresponding cell whereas the next time it deletes the cell follwing the one I clicked. The reason is that my button tag is not getting updated when I call the function reloadData().
Ideally, when I call the reloadData() ,
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
should get called and update the button tag for each cell. But that is not happening. Solution anyone?
EDIT:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
collectionView.registerNib(UINib(nibName: "LoveListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell")
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! LoveListCollectionViewCell
cell.imgView.hnk_setImageFromURL(NSURL(string: (wishlist?.results[indexPath.row].image)!)!, placeholder: UIImage(named: "preloader"))
let item = self.wishlist?.results[indexPath.row]
cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.borderWidth = 1
cell.itemName.text = item?.title
cell.itemName.numberOfLines = 1
if(item?.price != nil){
cell.price.text = "\u{20B9} " + (item?.price.stringByReplacingOccurrencesOfString("Rs.", withString: ""))!
}
cell.price.adjustsFontSizeToFitWidth = true
cell.deleteButton.tag = indexPath.row
cell.deleteButton.addTarget(self, action: "removeFromLoveList:", forControlEvents: .TouchUpInside)
cell.buyButton.tag = indexPath.row
cell.buyButton.backgroundColor = UIColor.blackColor()
cell.buyButton.addTarget(self, action: "buyAction:", forControlEvents: .TouchUpInside)
return cell
}
A couple of things:
You're doing too much work in cellForItemAtIndexPath--you really want that to be as speedy as possible. For example, you only need to register the nib once for the collectionView--viewDidLoad() is a good place for that. Also, you should set initial state of the cell in the cell's prepareForReuse() method, and then only use cellForItemAtIndexPath to update with the custom state from the item.
You shouldn't reload the data until the deletion is complete. Move reloadData into your completion block so the delete method is complete and the view has had time to update its indexes.
However, it would be better if you didn't have to call reloadData in the first place. Your implementation ties the button's tag to an indexPath, but these mutate at different times. What about tying the button's tag to, say, the wishlist item ID. Then you can look up the appropriate indexPath based on the ID.
Revised code would look something like this (untested and not syntax-checked):
// In LoveListCollectionViewCell
override func prepareForReuse() {
// You could also set these in the cell's initializer if they're not going to change
cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.borderWidth = 1
cell.itemName.numberOfLines = 1
cell.price.adjustsFontSizeToFitWidth = true
cell.buyButton.backgroundColor = UIColor.blackColor()
}
// In your UICollectionView class
// Cache placeholder image since it doesn't change
private let placeholderImage = UIImage(named: "preloader")
override func viewDidLoad() {
super.viewDidLoad()
collectionView.registerNib(UINib(nibName: "LoveListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell")
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! LoveListCollectionViewCell
cell.imgView.hnk_setImageFromURL(NSURL(string: (wishlist?.results[indexPath.row].image)!)!, placeholder: placeholderImage)
let item = self.wishlist?.results[indexPath.row]
cell.itemName.text = item?.title
if(item?.price != nil){
cell.price.text = "\u{20B9} " + (item?.price.stringByReplacingOccurrencesOfString("Rs.", withString: ""))!
}
cell.deleteButton.tag = item?.id
cell.deleteButton.addTarget(self, action: "removeFromLoveList:", forControlEvents: .TouchUpInside)
cell.buyButton.tag = item?.id
cell.buyButton.addTarget(self, action: "buyAction:", forControlEvents: .TouchUpInside)
return cell
}
func removeFromLoveList(sender: AnyObject?) {
let id = sender.tag
let index = wishlist?.results.indexOf { $0.id == id }
let indexPath = NSIndexPath(forRow: index, inSection: 0)
collectionViewLove?.deleteItemsAtIndexPaths([indexPath])
wishlist?.results.removeAtIndex(index)
}
It's probably not a good idea to be storing data in the cell unless it is needed to display the cell. Instead your could rely on the UICollectionView to give you the correct indexPath then use that for the deleting from your data source and updating the collectionview.
To do this use a delegate pattern with cells.
1.Define a protocol that your controller/datasource should conform to.
protocol DeleteButtonProtocol {
func deleteButtonTappedFromCell(cell: UICollectionViewCell) -> Void
}
2.Add a delegate property to your custom cell which would call back to the controller on the delete action. The important thing is to pass the cell in to that call as self.
class CustomCell: UICollectionViewCell {
var deleteButtonDelegate: DeleteButtonProtocol!
// Other cell configuration
func buttonTapped(sender: UIButton){
self.deleteButtonDelegate.deleteButtonTappedFromCell(self)
}
}
3.Then back in the controller implement the protocol function to handle the delete action. Here you could get the indexPath for the item from the collectionView which could be used to delete the data and remove the cell from the collectionView.
class CollectionViewController: UICollectionViewController, DeleteButtonProtocol {
// Other CollectionView Stuff
func deleteButtonTappedFromCell(cell: UICollectionViewCell) {
let deleteIndexPath = self.collectionView!.indexPathForCell(cell)!
self.wishList.removeAtIndex(deleteIndexPath.row)
self.collectionView?.performBatchUpdates({ () -> Void in
self.collectionView?.deleteItemsAtIndexPaths([deleteIndexPath])
}, completion: nil)
}
}
4.Make sure you set the delegate for the cell when configuring it so the delegate calls back to somewhere.
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//Other cell configuring here
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("identifier", forIndexPath: indexPath)
(cell as! CustomCell).deleteButtonDelegate = self
return cell
}
}
I was facing the similar issue and I found the answer by just reloading collection view in the completion block.
Just update your code like.
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
collectionViewLove?.performBatchUpdates({
self.collectionViewLove?.deleteItemsAtIndexPaths([indexPath])
self.wishlist?.results.removeAtIndex(indexPath.row)
}, completion: {
self.collectionViewLove?.reloadData()
})
which is mentioned in UICollectionView Performing Updates using performBatchUpdates by Nik

Resources