I am creating a view controller that holds a couple different fields for creating a log entry. To do this I have used a grouped table view with static cells, which looks like this:
Within each cell I have added a textfield (which has placeholder text as you can see from the screenshot). For the first text field I was able to create an IBOutlet for it using a subclass of UITableViewCell that I created, as seen below:
public class TextInputTableViewCell: UITableViewCell{
#IBOutlet weak var textField: UITextField!
}
However, when I tried to do the same for the second cell, using the following class:
public class MultipleTextInputTableViewCell: UITableViewCell {
}
It doesn't let me use-control click, or frankly any other method, to create an outlet for the text fields. I have tried setting the cell as a member of "MultipleTextInputTableViewCell" in the identity inspector, but that did not solve the problem. I have compared the two situations very closely and found no differences, so I am really confused to as why it isn't working for the second cell.
EDIT:
I was asked to post the attributes for the cell from the identity inspector. Below is a screenshot:
If one both cell are not static then it is possible. If you are use static cell then you can't use dynamic.
I think you should use dynamic prototype cell instead of static cells. It may resolve your problem :)
So I solved my own answer, but Stack Overflow won't let me accept it for another two days.
From what I can tell, this is an Xcode bug.
By manually typing out each outlet and then dragging the connection circle in the line-counter to each text field I was able to connect them. However, I tried it the other way around (option-click dragging from a text field to the written code) and that still didn't work. I am using assistant editor capability to do this so the issue may lie there.
EDIT:
Also, what actually would have been the accepted answer from me that I just found out (a couple hours later after I posted this question) is that you can create an #IBOutlet directly in the view controller holding the table view if you are using static cells. There is no need to create subclasses for each cell, and I guess Xcode is discouraging it through not allowing it to be easily connected. This connection works both ways and greatly simplifies my project. But kudos to myself I guess.
Related
I have multiple custom UITableViewCells, each containing a UITextField. On value change of any of those text fields, I want to update a UILabel elsewhere in the view. How do I do that?
The label displays an aggregate of all the text fields' values. The number of cells is dynamic.
(I guess that's very easy, but I'm new to Swift development. My first attempt was to add an #IBAction to the cell class definition, but I got stuck on trying to get a reference to the label.)
Edit:
The accepted answer is absolutely fine and works. I also recommend checking the solution recommended by DonMag in the comments section as it may be better in the long run, although it seems more difficult to implement for a beginner.
So I know that you could reference a UITextField that you created in the interface builder by creating a custom UITableViewCell class and connect it with an IBOutlet.
But is this good code practice? I mean I am basically just creating a new class for a UITextField. Is there a more suitable way to do this? AFAIK you can't set an IBOutlet to the UITableViewController itself since you can't reference objects on repeating content.
Yes this is a good practise. When I first started learning swift, I myself went through this phase because creating a new class overtime seemed a lot of work. But trust me, it is the best way. I learned swift from a book which I bought from AppCoda. It always emphasises on creating custom classes for tableView cells as well as CollectionView cells.
In your example you have 2 prototype cells, creating custom classes for both of them helps in differencing one cel from the other. Also, you will also not get confused between the two. This also helps in changing settings/appearance of cells as you like and with ease.
Hope this helps. :)
I decided today that using static tableViews would suit my app better than dynamic ones.
Each cell has a label, and a UITextField
I subclassed one of the static cells and then tried to create IBOutlets for the label and textField... only it wouldn't work. Zero IBOutlet functionality when it comes static cells apparently and their subclass
I can however drag in IBOutlets to the UITableViewController these cells are apart of
So, essentially I would have a UITableViewController with outlets for each cell, each cells textField, each cells label, and potentially any other properties I want to add to the cell
I didn't want that much annoying code so I tried using dynamics
I was able to create IBOutlets for each object the way you can normally expect
I again decided against dynamics UITableViewCells, and switched back to static...
ONLY the static cells now have IBOutlets connected to them (they didn't disappear or throw errors when i returned to static UITableView)
I can access the cells properties by using cell.textField which is a lot better than a billion IBOutlets for each object on the cell
My question is... Why can't I create IBOutlets on the subclass of Static TableViewCells
Is the way I did it the only way, or is there a better way? I would hate to keep switching back and forth, but it allows me to get rid of dozens of lines of code I'll do it
When you create IBOulet for a UITableViewCell and you try to connect them on your StoryBoard, go to the left bar (where the objects of your view controller are) and press Ctrl + Click over your custom cell. Then the IBOutlet object will appear in a popup and you will be able to link them.
Custom Cell For UITableViewCell
I'm developing an iOS application and i need to make a custom cell for my side menu, i have seen so many examples and i found out those two approaches :
Make a class (.h and .m files) that inherits from UITableViewCell and put the outlets in the .h file then apply the class on the cell and connect those outlets to the labels and/or images from your cell in the storyboard.
Or the easy way is to give a tag to the labels/images or whatever you have in the cell in your storyboard.
My Question: I'm wondering which way is more accurate and professional and used by most iOS developers ?
Definitely the first one. It's clear and maintainable in the future. The purpose of the tag is not to store arbitrary values. See my answer regarding this.
This article has some details on the usage of tags
I'm creating a Table View in Interface Builder (Storyboard). I'd like to have a couple of different Dynamic Prototype cells with different sets of Labels and Images in them and so on, and I can give them different reuseIdentifiers so I can pick which ones I want at runtime.
In Interface Builder, I create several Dynamic Prototype cells in my UITableView, which is controlled by a UITableViewController.
In the first cell, I drag and drop in various views and so on.
In the second cell, IB will not let me drag any views into it? I can resize the second cell vertically, but can't put anything into it at all, either by dragging into the cell or into the object graph in the left-side bar.
If I copy and paste the first cell, a second Dynamic Prototype will appear with all of the same contents, but I won't be able to modify the copied cell (can't add or move subviews). However--and this is strange--I can select the constraints and modify their values to resize and shift objects in the second cell.
As a note, running XCode 5-DP3. Tried restarting it (didn't expect that to help, and it didn't). Otherwise, unsure what to try, and unsure if I'm doing something very braindead, or if this is a bug I need to report to Apple.
So, am I crazy? Has anyone experienced this/can anyone recreate this?
EDIT:
After further testing, if I stick a big UIView into the first cell, and then copy that cell, I can edit inside my added view. (Does this make sense?) I can't edit anything that lies within the second UITableViewCell, but if it contains a UIView copied over from the first cell, I can put new views into that view and move them around and so on. Super-strange.
For the sake of posterity, I'm answering my own question:
The way I solved this was to take a UITableViewCell object from the Object library and drag it onto the UITableView. Sounds simple, right?
The problem I was running into was only if I copied existing dynamic prototypes through Cmd+C & Cmd+V, or by incrementing the number in the Attributes inspector for the table view. The Storyboard Editor wouldn't allow me to modify those ones.
Dropping in new cells from the Object library let me tweak them all separately.
XCode 5-DP6 solved issues with not abling to resize cell's subviews.