Create properties for Static UITableViewCell Subclass - ios

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.

Related

UITextField in UITableViewController

I can't find a question like this. I searched all of the Internet but nobody ask this question. Even Apple's tutorials, there is no example. Here is my what I want to do. I want to create an app like Reminders or Wunderlist. I want to emphasize the UITextField of UITableViewCell's first cell as text input.
My first cell is a static cell which is UITextField and the other cell's are the results of UITextField. For example:
Static cell (UITextField)
dynamic cell (Label)
dynamic cell (Label)
...
There is no button for UITextField. I want to get input from keyboard's return like Reminders.app due to I must implement UITextFieldDelegete and equalize in viewDidLoad like textField.delagate = self. The problem starts here. I can connect outlets in UITableViewCell (for UITextField and cells' labels) but in UITableViewCell's instance method, there is no viewDidLoad method and it disallow to use UITextFieldDelagete which can require to get input without using any button. If I use ViewController (not UITableViewController), I can easily connect outlets and use UITextFieldDelegate. How can I solve this problem? Which point I missed?
Please Check Screenshot how to add any Control into UITableViewController
do not forget to change Static cell instead of dynamic prototype in tableView Property.
IBOUTLETs will connects with your ViewController , not with Custom Cell.
How to Set OUTLEts in TableViewController :
ScreenShot
Please feel free to ask

Custom UITableViewCell - subclass UITableViewCell with xib vs using tags

In order to implement a custom UITableViewCell, I know of two options:
Create a prototype cell in the storyboard view and assign tags to the cell's constituent views
Create a subclass of UITableViewCell with an associated xib
Using the tags seems simpler. I see that it has a couple of disadvantages including the inability to re-use the custom cell in more than one scene and potentially a lot of cell specific code in the view controller which could be an issue with a bunch of different cells.
For one or two cells that do not need to be reused in multiple scenes, does anyone know of any other considerations that I should take into account that might lead me to prefer a UITableViewCell subclass?
My recommendation is to use a UITableViewCell subclass with a storyboard prototype cell. Simply link your constituent views to IBOutlet properties as you would with any other class.
If you want to use the same cell in multiple tables then you need to re layout the prototype cell in each tableView, but you can cut and paste. Using tags results in unnecessary code to get references to the UI elements.

Custom UITableViewCell using storyboard with some cells

I have a TableView in which a most cells are pretty standard. I make them buy using static cells in Storyboard. However, one cell I would like to customize probably using an XIB file so I would need to load it programmatically.
In the TableView's data source, is it possible to handle loading XIB view for this particular cell only, while leaving other cells to what's delineated in the static cells in the Storyboard? Or is it an all or nothing thing where I need to just give up using static cells altogether?
The rationality for doing this is that I would like to make Storyboard to look as close to the real thing as possible. Right now if I provide a data source, the static cells in the storyboard would have no effect on the actual output and is not in any sense linked to the actual output.
Yes, it is possible. Set the custom class for the custom cell. If you wish to customise it from code, just connect it as an IBOutlet to the UITableViewController.

How to display label in UITableView

I created a UIViewController using story board in Xcode 4.2. Then added a UITableView object to it by dragging it on to the view controller. Then dragged a label to one of the cells of this UITableView and made label's text to be MyFirstLabel. Now when I run this in simulator I am not able to see the label. What am I missing ?
You must create properties for UILabel when adding to cell
let's say you have a UILabel declaration like this(and connected to the object you drag on storyboard)
#property(retain, nonatomic) IBOutlet UILabel* yourLabel;
in your .h file and
#synthesize yourLabel;
in your .m file. After initializing the cell(on the cellForRowAtIndexPath delegate method)
add your label to the cell like this [cell.contenView addSubview:yourLabel]; I hope this helps-if not, please give some more details about your problem
You should see the label. Make sure you have static cells for your table view. You do this by selecting the table view and change this in the inspector. Now you can create the exact number of sections and rows, including the content, right in storyboard.
If, however, you want to change the content of the label, you have to create an IBOutlet, still using static cells.
If you want to vary the number of sections and rows you will have to go with dynamic cells. In this case you should see the label if you have the correct Cell Identifier set in storyboard and referenced in your table view datasource method cellForRowAtIndexPath.
EDIT:
As has been pointed out, you cannot use static table views if not embedded in a table view controller (which claims the whole screen). So here is the workaround:
Option 1: you make it into dynamic cells as mentioned above and implement the datasource protocols in your ordinary view controller. You could then insert the label in code.
Option 2: as option 1, but with subclassing UITableViewCell where you can design the cell with a xib and create the necessary outlets.

confusion about static cells vs dynamic cells

I'm trying to figure out how to have a few static cells in addition to dynamic cells (I think) but when I start a new Master-Detail app and switch the default Dynamic Prototypes to Static Cells, it crashes.
I think, since I'm still new at this, that I don't understand how all of the components (table views, cells, delegates) are wired. After I switched to Static Cells I made three cells but they don't show up. The app just crashes.
I can include code but basically, I create a new Master-Detail, switch to Static Cells, change the name of three cells and it crashes (in the main loop).
Thanks ahead for everything.
If I understand your question, this is the explanation you are looking for:
Prototype (reusability):
When you use Dynamic cells, you use Prototype cells. That means either in IB or in your code, you create an instance of UITableViewCell, and give it an identifier (reuseIdentifier). The UITableView uses this prototype to generate as many cells (rows) as you need. UITableView create (and use memory) for as many cells as it needs to fill the screen. Once some of these cells go off the screen, UITableView reuses them -- recycles them.
You can define more than one prototype cell in a UITableView. The idea is that each prototype serves a different purpose. For example, you define a prototype cell that has only one big UILabel and its purpose is to use static text. You define another prototype that has only one UIImageView. It depends on your design and how you want to display your data.
To use prototype cells, in IB, you use Dynamic Prototypes cells, and set an identifier. Then you have to implement UITableViewDataSource methods, such as:
– tableView:numberOfRowsInSection:
– tableView:cellForRowAtIndexPath:
These methods are delegate methods of UITableView.
If not using IB, you would create cells in – tableView:cellForRowAtIndexPath: method:
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier];
Static:
The idea of static cells is just simplicity. You can use the prototype cells (e.g. only one prototype) and feed in a static NSArray, for example, as data source. To take the task of implementing UITableViewDataSource methods, Apple introduced static cells, where you would only use the storyboard and IB. In IB, you select the table view, and choose Static Cells instead, and type in your text, or set the image.
You don't want to make any dynamic changes to static cells later when the app is running because it is not meant to, and you would have to implement many more methods that it is not worth it. Although it not much of a work to get the indexPath of the selected row, but the idea is if you need to push a new view from selection of a static cell, you would use segues, instead of implementing any code.
When using static cells, you should not implement UITableViewDataSource method, otherwise your app crashes, and vice versa, if you do not implement UITableViewDataSource methods (required ones) when using prototype cells, your app crashes.

Resources