Comments Custom Delegation
Instagram Firebase
Let's now move onto the beginning of our implementation for showing Comments inside of our application. First we'll look at how to implement Custom Delegation that allows us to tap on an icon in a cell and propagate it up to the controller. Furthermore, we will discuss how to find out which particular list item you are clicking on.

Comments (19)
GeminixClown
6 years ago
This is really important lesson, a long time ago, i need probably about a week to figure out how to bring data from cell to the view controller, and believe me guys, almost any courses don't teach this method... awesome implementation Brian as always....
fahad
6 years ago
Great lessob Brain! Is there way we can implement the push controller inside the collectionViewCell?. Thanks!
fahad
6 years ago
lesson*
Brian Voong
6 years ago
fahad
6 years ago
Thanks!
albaqawi
5 years ago
BINGO this is the core lesson and take away with custom delegation in a ViewController.... you just summarized it with perfection. thanks :D
ivancantarino
6 years ago
Let me tell you Brian, this lesson has some serious content. I struggled so much before to pass data between cells to the VC's; I've instantiated super-global variables to store the desired information; I've implemented singletons that would store the info, but never remembered to use a Protocol. It's something so powerful and from now on it will be part of my development guaranteed
Brian Voong
6 years ago
Jesus Adolfo
6 years ago
omg 6 videos left :(
Kenny Ho
6 years ago
Hey Brian, found an extension to change background color of the status bar. extension UIApplication { var statusBarView: UIView? { return value(forKey: "statusBar") as? UIView } } *on AppDelegate* UIApplication.shared.statusBarView?.backgroundColor = .red Hope this helps!
Kenny Ho
6 years ago
that comment was suppose to be on "Customizing the Navigation Bar and MVC Implementation" for the YouTube tutorial. oops lol
Brian Voong
6 years ago
stonypig1
6 years ago
hello, eveyrone i am a newbie, just learned swift couple months ago. i just watched this video, i am not fully diguesting everything Brian said, but try the best i can to use as much code as possible. my code still have UI and storyboard, but i use the Brian's method here and i add prepareforsegue method in the viewcontroller to pass the postuid to the new viewcontroller, is there any downside of this when coding ? or as long as works is fine ? thank you in advance.
Brian Voong
6 years ago
stonypig1
6 years ago
i may sounded easy, but it took me about 7 days more than 40 hours to accomplish wut you spent 10 mins in that tutorial, hahaha, but at the end it is fun and felt nice, and thanks for your encouragement to newbie programmers
weare99
5 years ago
Hi Brian, custom delegation is a really nice topic. But how can I handle custom delegation with multiple cell ? For example like this: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let identifier: String if indexPath.item == 0 { identifier = topCellId } else if indexPath.item == 1 { identifier = middleCellId } else { identifier = bottomCellId } let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) return cell }
weare99
5 years ago
:) Here is the solution sorry: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let identifier: String if indexPath.item == 0 { identifier = topCellId } else if indexPath.item == 1 { identifier = middleCellId } else { identifier = bottomCellId } if identifier == topCellId { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topCellId, for: indexPath) as! TopCell return cell }else if identifier == middleCellId { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: middleCellId, for: indexPath) as! MiddleCell return cell }else { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: bottomCellId, for: indexPath) as! BottomCell cell.delegate = self return cell } }
salar Soleimani
5 years ago
Hi Mr.Voong I'm using your library(LBTAComponent) instead of raw collectionView. Please tell me how can I handle custom delegate in Datasource and DatasourceCell?
Brian Voong
5 years ago
silviu.isidor@emanuel.ro
4 years ago
Hey Salarying, Use this code: override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> DatasourceCell { let cell1: PostCell let cell2: UserCell if indexPath.section == 0 { cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass((datasource?.cellClasses()[0])!), for: indexPath) as! DatasourceCell as! UserCell cell2.datasourceItem = datasource?.item(indexPath) return cell2 } else { cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass((datasource?.cellClasses()[1])!), for: indexPath) as! DatasourceCell as! PostCell cell1.delegate = self cell1.datasourceItem = datasource?.item(indexPath) cell1.controller = self return cell1 } } I am using the IndexPath.section to target different DatasourceCell types. Does it work?
salar Soleimani
4 years ago
Sanzio Angeli
5 years ago
Hey Brian! Is it customary to always set the delegate to self? I have a UIImagePickerController singleton that I create upon launch of my app. I was shooting for a global picker so the user wouldn't have to wait a second or two every time we wanted to bring up an image picker. I want to set different delegates to globalImagePicker.delegate to change its functionality. My problem, I believe, is that .delegate property of UIImagePickerController is a weak var and I cannot maintain a strong reference to it. This got me thinking if it is ever recommended to set delegates like this? I believe it will clean up my code.
OfftheCode
5 years ago
Love this course, also Brian <script> alert("Pls dont fail for it") </script> I'll delete this comment either it works or not | testing cases
OfftheCode
5 years ago
Can't delete but happy it didn't work out, thanks for great security!
shubhamsaurav
4 years ago
if want to pass the post photo to comment vc .. how i can do that ??
mao
4 years ago
Hello Brain, I made a BaseCell(4 cells like the BaseCell UICollectionViewCell in your youtube app Video) which can be swiped horizontally. then I wanted to make 4 UICollectionViewCells which contain different contents, so when the users swipes left and right, they can see different images/layouts. one of 4 CollectionViewCells contains the content similar to HomePostCell here. My question is What should I write under "func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {" to make the Custom Delegation work based on MVC? and I don't know where I should input "cell.delegate = self"
roygbiv
4 years ago
Hi Brian, From what I read about lazy var, it has something to do with preventing unnecessary memory allocation. So in this case, we set all the buttons to lazy var such that only when the user taps on the messageButton will the messageButton object actually be created and allocated a memory ? Is that right ?
Brian Voong
4 years ago
rehan1531
4 years ago
Brian you are amazing
Stephan Dowless
4 years ago
email me at dowless.stephan@gmail.com for how to paginate home feed, implement hashtags/mentions and more
tsangaris
4 years ago
Sweetest delegation explanation ever!
MaxApp
4 years ago
Hey Brian, Is there any way that let me download your tutorial because I will be traveling next month and the flight is about 9 hours. I know you are very limited on time and I'm sorry if questions are not related to the subject.
Cinquain
4 years ago
Yeah I agree. You should let us download your videos. I’m tired of depending on WiFi cause I travel a whole lot
Brian Voong
4 years ago
Cinquain
4 years ago
You got a lot of work on your plate Brian.
Cinquain
4 years ago
Fire
Kyle Griffin
3 years ago
Amen.
HELP & SUPPORT