UITextField in UITableViewCell's and proper usage - iphone

I have a complex settings style table where individual cells represent different aspects of a data model class. Users can click into a cell and edit individual attributes, such as say if I have a user class, a name, date of birth, etc. My question is, do I need to have an instance of UITextField for each unique cell? Can I just create one subclass of UITableViewCell, set up a delegate, and determine where it is from there?
What's the best approach?

I would recommend creating a subclass of a UITableViewCell. You could do this either purely programaticaly, or if you have an aversion to CGRect's (or want to be able to drag and drop your layout around) with a combination of a XIB and a custom class file.
The Subclass would then contain the UITextFiled's you need, and can also have a delegate or datasource that you can use to point it to your data model object.

It's better to have the UITableViewController you are using act as the text view delegate for each cell - make sure you are re-using cells and when you create them or reuse them attach your class as the delegate for the UITextViews you have via a custom UITableViewCell class with accessors to get to the UITextViews.
If you set cell classes as text delegates you may run into issues if the user scrolls a table view cell off screen with the keyboard up.

Related

UITextView in UITableViewCell subclass, how to keep the data separate from the view

So I don't know what the best way to follow MVC is. Similar to the address books app, I want to have a UITableVeiewcell that has the ability to edit notes. I figured I would do that with a UITextView in a UITableViewCell subclass. My subclass has just that as a property, and a label that says "notes". I can see a few use cases that I need to consider,
1) when they are done editing and click outside or hit return.
2) when the text goes beyond the size of the cell I need to resize the cell.
Because my UITextView is in IB, is there a good way to define the delegate methods for the UITextView since my UITableView is in another ViewController subclass? Like how do I pass that information back?
Or, is it better to create my UITableViewCell subclass in code since it's just a couple of items so all my delegate and resizing code is done in the view controller class?
Thanks!
After text field editing was finished, you can store it's value in some dictionary in your controller. You can use cell's indexPath as key in this dictionary. In such way you will not lose your data with dequeue cells.
To resize cell you must call reloadData method and change rowHeight property of entire tableView or implement tableView:heightForRowAtIndexPath: delegate method to set needed row height to current cell.
I haven't use UIKit since iOS 3.1, so the second part of my answer can be out of date, but I hope it will help you =)

table view cells with different controls

I was working with the grouped table view , and i wanted different controls for every row i.e switch control for 1st,radio button for 2nd ,checkbox for 3rd and so on.. how can this be implemented programmatically that is without using interface builder
thanks in advance
CharlieMezak said is right, you need to create in UIControls directly in cellForRowAtIndexPath , and add as subviews to contentView of the cell
For reference see the link below
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
the link specifies the code to create cells programmatically as well as using IB.
Table View Programming Guide for iOS
Read the programing guide, and remember to use different CellIdentifier for each type of cell.
This is a pretty vague question.
Obviously you need to provide the cells to the tableview in its cellForRowAtIndexPath delegate/datasource method. So, either in that method or during the initialization of your view controller, build the UITableViewCell instances that you need, adding the various controls that you want to them as subviews and connecting the controls to your view controller so you can detect when they have been changed. Then just return the appropriate cell in the cellForRowAtIndexPath method.
Personally, I think it's a lot easier to use IB in cases like this. Just create an IBOutlet instance variable for each custom cell you want, and return the right cell in cellForRowAtIndexPath.

Can I subclass UITableView to make it a specific style?

Basically I want to re-style UITableView(Controller). Each cell will have a picture as a background and such. this tableview will be used throughout the app thats why I want to subclass it, because I want to be able to define the style once and then reuse it everywhere, but still be able to change things per view. For instance, I'd like every cell to have the same blue background and a specific font, but I might want to change the font in one view, or make the cell taller/shorter. You get the idea.
Can I subclass UITableViewController and create the style in the methods in that? Then subclass MyCustomUITableViewController when I want to create a table view? Or would it better to subclass UITableView and then [self.view addSubview:(MyCustomTableView *)tableVie]?
Thanks
Tom
You will probably want to subclass UITableViewController to define relevant UITableViewDelegate methods, but I think that the real work will be in subclassing UITableViewCell to get the look that you want.
UITableViewCell is a UIView, so it can have a background color and it can contain a UILabel, for example.
Your -tableView:cellForRowAtIndexPath: will need to instantiate your UITableViewCell subclass (or load it from a nib) - and then configure it appropriately. I.e., setting text, image, font, etc.
Hope this helps.

How to do edit-in-place in a UITableView?

Is there a standard way to set up a table to allow editing-in-place, kind of like this:
I only need editable text at the moment, but I might need UISwitches or UISliders in the future.
Yep. Just add a UITextField, with its font and textColor set to appropriate values, as subviews of the table cell's contentView. You probably want to give the field a tag as well, so that you can easily grab a reference to it using the contentView's -viewWithTag: method.
With short forms you can get away with keeping an array of cells, one for each field, and handing them off to the table view without going through the -dequeueReusableCellWithIdentifier: mechanism, but if you've got a lot of stuff to enter then it gets more complicated. In that case, you probably want to assign a different reuse identifier to each type of cell—one for text-field cells, one for switch cells, one for slider cells, etc. Once you've dequeued or created the row's cell, you'd then grab its control from the content view (as above) and set its value from wherever you've stored that.
Edit in-place using a "Content: Static Cells" and make it look like a default styled UITableviewCell.
In UITableView create two cells: one Custom and one Subtitle Styled.
Copy the "title" label corresponding to the Subtitle styled UITableViewCell to the Custom UITableViewCell.
In Custom UITableViewCell, drop a UITextField. Copy in this the style parameters from the Detail UILabel from Subtitle Styled UITableViewCell in this UITextField.
Do the necessary arrangements.
The looks pretty equal, aren't they?
Now, do the IBoutlet stuff.

iPhone -- customizing a grouped table view in Interface Builder

I am writing an iPhone app. According to the design, it is supposed to contain a lot of grouped table views. In these views, the rows are frequently not similar to each other. For example, on one screen, one row is the name of a task, another is its owner, another is its description, yet another is its history (which is supposed to be an expanding box), and so on. These rows are edited in different ways. For example, the name can be entered free-form, but the owner has to be chosen from a list, which would be brought up in a further table view.
I think doing all of this programatically would drive me batty. What I want is a way to design these screens in IB. But I can't figure out how to get IB to do treat the cells individually. Is that possible?
Thanks.
In Interface Builder you create custom UITableViewCells for each row and then return the appropriate custom cell in – tableView:willDisplayCell:forRowAtIndexPath:. You also need to return the height of each custom cell in – tableView:heightForRowAtIndexPath:.
Within Interface Builder, you create a custom cell just like you would any view. You size the cell and then fill it with subviews. It's best if you create a UITableViewCells subclass for each custom cell that has IBOutlets that connect to each subview. That way you can easily access labels, imageviews, controls etc.