UICollectionView, button not appearing - ios

I am still fairly new to programming and I do not quite understand UICollectionViews. Can a programmer add buttons in the cells of an UICollectionView? Or only UIImages? I added a button into one of the cells and when I ran the iOS simulator nothing appeared. I also just dragged in an UICollectionView and I am not using an UICollectionView view controller. I dragged the UICollectionView into code as an outlet too and constrained the view, cell and button. Attached is a screenshot of my view controller and iOS simulator. Thanks!!

I was recently new to collection views, and they can be confusing. I found a video tutorial that explains it all really well, but it was not updated for Swift 3 so I wrote made this GitHub project to help explain it to new users.
You can certainly add buttons to to the cells of a UICollectionView. Each cell is like a little UIView in and of itself. However, I believe you need to create a new class for the cell and create an IBOutlet for the button. You also need to create a couple of functions inside your main ViewController.swift file to actually view the UICollectionView. It can be complicated at first, but stick it out! In a little while, you'll be able to do them with ease. Follow the tutorial in a new project to practice, then you might figure out what the problem is.

Sorry that that last one was unhelpful, I'm new to this. Thanks for the tip, manetsus!
I created a new project and tried to add a button to the UICollectionViewCell. Here's what I did:
I added a button to the cell, then created an IBAction by control+dragging from the button to ViewController.swift. I ran it, and it worked. Each button within the UICollectionView cells performs the IBAction in ViewController.swift.
If you want cell-specific actions, consider using:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
//Do something with the cell...
}

Related

UICollectionView cell apparition animation

I am trying to animate the first apparition of a cell in a collection view as simple as a ZoomOut + Fade IN of the cells when the collection view is first called.
I have NOT subclassed UICollectionViewFlowLayout so far as I did not think it was necessary for such small action and I do not need to further invalidate the layouts once the cells are displayed. There is actually no animations of the cell beyond their first apparition. Reading the apple's developper guidelines on the subject seems to confirm I should not go to the subclass route but still I have not found how to perform the animation I want.
I have played along the lines of defining the cells using UICollectionViewDelegateFlowLayout methods then performing a CGAffineTransform sequence in collectionView(_ collectionView: willDisplay cell: forItemAt indexPath: IndexPath) of UICollectionViewDelegate and the proper transformation does occur. Then I was thinking of reversing this transformation by calling a simple CGAffineTransform.Identity() in an animation but I have not found yet where (in which of the CollectionView classes method) to place it.
Thank you in advance for your king help!

Is it language appropriate to layer a UIButton over a UILabel in Swift 3.0?

My question is this: Is this following approach to making a UILabel tap sensitive stylistically acceptable in the Swift 3 language? I'm tempted to say "if it compiles it flies" but I don't want to get in the habit of using this "short cut" if it is going to bite me later on. Further, if it is acceptable, are there some drawbacks to using this method that aren't obvious to a newcomer like me?
Please note that I am not looking for a way to implement code, I have one. I am asking if the solution I have is acceptable from a language style perspective.
I've been trying to get a UILabel to accept a TapGesture when inside a table cell for 2 days now and whatever method I try, there is always some sort of error even if it will compile. On a hunch, I went to my storyboard for the table view and added a button on top (not stacked, or aligned, or anything like that – actually occupying the same 2D space on the story board) within the prototype cell. I deleted the button text, linked it to the table view cell code and implemented some basic functionality to change the text on the UILabel to red and back. All of this functions exactly like I expected. I click the button and the text changes from black to red, and back when clicked again. The UILabel text is static in the table cell on a white background and my real function isn't going to change the text, it is going to alter another view through delegation from that view.
Why do it this way? Even if I check the UILabel use interaction box and follow some of the other questions and answers here to make it tap-able, I cannot control+drag the UILabel to the table view cell and make an action, the option simply doesn't exist in the pull down menu. It is available if I control+drag the UILabel to the table view controller. This makes a kind of sense to me because it is the table view controller that senses touch (right?). But, on the other hand, I have a switch in the table view cell that works just fine when I follow the answer to this question. Simple functionality of the storyboard-code interation (control+drag) is preventing me from getting what I want. Maybe control+drag should be allowing me to make an action and doesn't? I don't know. I don't want to use a UIButton alone because the text scaling feature of UILabel is really handy.
If you just want to recognize taps on the table cell, make the class the delegate of the table view
tableView.delegate = self
and implement
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//your code here
}
I'd also appreciate you taking the time to read a bit more about UITableViews. Every UITableViewCell has a label by itself. Consider the following code:
tableView.dataSource = self // this code will ideally be in init
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "lovCell")
cell.textLabel?.text = dummyData[indexPath.row]
return cell
}

How to assign a specific ViewController to a UIView (in UICollectionView)?

I searched the web for hours but I can't find a solution to my problem.
I have a View in my storyboard that contains a UICollectionView. I've set the cells of the UICollectionView to be really big (only one can appear on the screen, you have to swipe horizontally to see the others). In my application, I use this layout to ask the user some questions, one after another. The user can respond to those questions by touching buttons, move around a map, select a date, etc... Each questions have the same layout; they are displayed in a box, with a title label and a button. That's why I chose to use a UICollection View.
I want to display a specific view (the options that allows the user to answer (buttons, Map, DatePicker, etc..)) for each questions in the blue rectangle (see picture below).
A screenshot of my view
My idea was to create multiple ViewControllers in my StoryBoard and be able to tell : "ok, for the first question (first cell), put the view XXX in the container, for the second, put the view YYY, etc...".
How can I do this ?
I've tried to add a ContainerView but we can only link one view in the storyboard.
I want to able to do something like this :
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! SearchQuestionBox
cell.questionAnswersZone.view = storyboard.viewNumberOne //Just for the idea haha
return cell
}
I would suggest not using multiple view controllers.
Instead you could just add the views for each cell to the one main view controller. Keep in mind you can set a views frame to be offscreen.

How to have DidSelectRow activate keyboard?

Goal
I want to have a keyboard (custom) to show when I click on a cell in my tableview and have said keyboard edit a label in the selected cell.
What I have read and tried
Stack overflow and other searched threads/tutorials
A Swift example of Custom Views for Data Input (custom in-app keyboard)
How to make custom keyboard only for my app in Swift?
iOS 8: Creating a Custom Keyboard in Swift
Along with other results and searches (also the recommended readings within these threads), these were great for me getting the keyboard the way I want it (which is app-specific, I don't want to have the user install the keyboard to use the app), however, none explain how I could "activate" the keyboard without a textfield.
My thought process is this: I will have a keyboard with a textfield in place in order to receive the input from the keys pressed. This input would then be sent to the label that is in the selected cell. The problem is in the keyboard showing without anything to call it...
Why I don't want a textfield in the cell: Because I think it is more smart/elegant to have the tableview cell activate the keyboard. I have seen this in other apps and I can't figure out how it is done.
Any help on this matter is much appreciated. I am completely new to programming, am using Swift (so have no clue Obj-C or the like).
Thank you!
Well after a lot of discussions around other websites, it is a shame to say it but what I wanted was actually impossible (like stated by Duncan). For some reason I thought that in programming that word did not exist, but alas it is there. Well, the way that I did work around this limitation was as replied here in the comments as well as from the resource materials I already read:
I have 2 views:
tableView - this will have the list of items that I would like to edit a value. Has multiple labels where one of them would be edited by the user.
keyboardView - custom keyboard that would allow user input.
For the keyboard I used the delegate method as described in "A Swift example of Custom Views for Data Input". This worked perfectly and would send the data over to the tableView under the keyWasTapped function.
Next was the tableView. I had 3 labels in the custom cell, where one was called valueLabel. I had to add a textField to my storyboard hidden behind the tableview with which the user will interact and in the didselectrow I included the command to summon the keyboard:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
textField.becomeFirstResponder()
}
From there I would grab the text from the textfield and update the UILabels in my custom cell. At least this way I can select a cell and not the textfield. So considering that the answer that would fit what I wanted is combination of the replies, I thought it would be best to say it here.
Thanks to Sandeep and Larme for their time.
TVDN,
Have a textField in cell not the label :) Create a custom cell create an IBOutlet to that class ffrom textField in cellForRowAtIndexPath
let tableCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! myCell
tableCell.yourTextField.resignFirstResponder()
and in didSelectRowAtIndexPath
let tableCell = self.tableView.cellForRowAtIndexPath(indexPath)
self.tableView.dequeueReusableCellWithIdentifier("cell") as! myCell
tableCell.yourTextField.becomeFirstResponder()

Cannot add a PrototypeCell to UITableView inside xib file

I am new in IOS. I want to create a UITableView in a xib file instead of my Main.storyboard.
But the problem is when I drag a UITableViewCell in to UITableView, it does not work like in Main.storyboard.
My Main.storyboard now have many screen so it's very slow when load and also it's hard to find any screen. Therefore, I want to put put UITableView to xib file.
Do I miss something or I make something wrong?
Any help would be appreciated
Here is the describe image
I'm sorry, but prototype cells are available only in storyboard-based projects. Hopefully you won't have gone too far down the xib approach and can try multiple storyboards instead. If you find them a bit overwhelming, it's OK; they get better – note that you don't have to have one storyboard for all your view controllers. You can have several, either linked in code or using storyboard references.
If you want to stick with xibs, another option is to use registerNib in code. So you design your prototype cells in xibs, then register them in code. Not quite as smooth, but it might suit your needs.
Please check out this process.Follow this and you will get a custom cell to table
If you can use a regular UITableViewCell, simply register it.
In viewDidLoad:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "YourCell")
In cellForRowAt indexPath:
let cell = metricsTableView.dequeueReusableCell(withIdentifier: "YourCell", for: indexPath)
If you want to add one prototype cell, can add a UITableViewController to your xib, which has 1 prototype cell initially. This is not the best way (or maybe correct one) but can used in this case.

Resources