How to nest a UITextField within a UITableViewCell? - iphone

I'm just starting out working on my first iOS app. How do you create a UITableViewCell that contains a UITextField that looks like the Title and Location fields when adding an event within the Calendar application? Are there any handy third-party components for doing this?
I can see that the table view has two grouped items and that the text fields have some placeholder text, it's more about how to go about making the text fields take up 100% of their parent table view cells.
Thanks in advance for any help.

You need to add the UITextView as a subview of the UITableViewCell. In the cellForRowAtIndexPath: method do the following:
Create a UITextView as you normally would:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,300,40)];
NB: the numbers are the x, y, width, height. Modify these to fit your own app.
Add it as a subview of the cell:
[cell addSubview:textView];
If you only want specific cells to have the textview you will need to do something like
//Use the if statement to specify which row(s) you want the UITextView to appear in
if (indexPath.row == 1) {
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[cell addSubview:textView];
}

The Table View Programming Guide for iOS has a section called A Closer Look at Table-View Cells. It describes two techniques for customizing a table-view cell: with code and using a pre-built nib. I think it has a lot of information you will be interested in.

Related

Custom UITableViewCell with Storyboard

My goal is to create a custom UITableViewCell which contains 2 UILabels. One is the title and one is the detail. The detail label should allow for 3 rows of text.
So I went on and created a custom table view cell in storyboard. I also created a subclass of UITableViewCell and linked the two together.
I the added two UILabel to the cell in storyboard and placed them where i wanted them to be and linked them to their coresponding outlets in teh subclass. Since the content of the labels varies I wanted to align the text vertically to the top. As I understand the only way to do this is by calling the sizeToFit method on the label. I execute this under in the sub class of UITableViewCell:
-(void)layoutSubviews {
[super layoutSubviews];
[self.detailTextLabel sizeToFit];
}
So far everything seems fine and the text in the detailTextLabel is aligned as it should. Although when i satrt interacting with the cell, for example slide my finger over it so the delete button appears, the detailTextLabel will change size to the size that was set in storyboard. This causes the text to be misaligned. Similar things happen when i select the cell and change to another view and the return to the table view via a tab bar
My question is: Is there any way of creating this custom cell differently using storyboard or is my only alterative to create everything programtically?
Regard, Christian
Maybe you should take a look at this if you still want to vertically align your text in your UILabel without sizeToFit (who will resize it when you will interact with your cell).
About your question, I think you can create your custom cell from a xib file like this.

Adding UIPickerView to a UITableViewCell

I want to add a UIPickerView to a UITableViewCell. Right now I am just getting a black window. Can someone also explain me the concept of subclassing UI objects to a cell ie:when do we make our cell as the delegate and the datasource delegate? Thanks
EDIT: Here is the code of what i am doing right now
case 1: {
cell = [tableView dequeueReusableCellWithIdentifier:#"groups"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"groups"];
}
/*NSString *myGroupDetail = [_groupArray objectAtIndex:indexPath.row];
NSLog(#"the group detail label is %#",myGroupDetail);
cell.textLabel.text = myGroupDetail;
*/
[cell addSubview:_groupPicker];
break;
}
The groups is the section in which I want the picker view, I am getting that data from an array.
you would have to allocate and initialize a pickerview in your cellForRow method of the tableviewdelegate. ill sketch it for you =) how to initialize a cell itself should not be hard to find out if you google a bit ;-)
...(tableView *)... cellForRowAtIndexPath... {
if(indexPath.row == pickerRow){
UIPickerView *pickerView = [[UIPickerView alloc]init];
cell = ... // alloc and initialize a cell
cell addSubview:pickerView];
}
else{ // your other cells }
return cell;
}
UPDATE: im currently having trouble with git, so i uploaded a sample project to my private server: UITablePicker example
github: https://github.com/sebamisc/UItableViewWithPicker
yu can modify and use the code however you want =)
sebastian
Well, I never did exactly that.
Does it have to be in a cell or could you use the table's header or footer view for that? (I would not suggest a section header/footer view.)
Assuming it is within a UITableViewCell.
Yes, I personally would subclass UITableViewCell. Did that a lot. In that case you cell object could be the data source delegate of the picker. For doing so your subclass of UITableViewCell (let's assume you name it MyTableViewCell) needs to fulfil the related protocol. You add that UIPickerView programmatically within the init Method (initWithStyle) of MyTableViewCell. For the layout within the table cell, you should overwrite the method layoutSubviews. If your app can be rotated to landscape and portrait orientations and/or if your app is designed to run on iPad as well, then this method should dynamically consider the table's bounds. (Screen or windwo bounds are often used hiere but that is not save when the table is displayed within a split master view or a popup view on iPads.)
Strictly spoken your MyTableViewCell should not be the data source delegate, simply because it is a view element and view objects are not supposed to manage any business logic within an MVC design pattern. Smarter would be the implementation of some dedicated view controller for your table view cell hat fulfills the protocol and is assigned as the delegate. However, both would work. In the event that it is the only picker view within your sell, then you could easily use your UITableViewController subclass even without tagging the UIPickerView.
An alternative to subclassing a UITableViewCell is to create the UIPickerView within the cellForRowAtIndexPath method of your tableViewController. That is fine for singe-orientation apps. However, you may setup it in a way that it re-arranges its the UIPickerView automatically.
In any case you should overwrite the heightForRowAtIndexPath method of UITableViewController when your table views do not have the same hight any more. If all of them still have the same height, then you can simply set the rowHeight property of your UITableView.

TableView,label and Textfield on ScrollView

In my app I need to do something like this:
First of all I have a tableview that contains 3 section (2 of them with 3 rows). Below this tableView I have to put a label,and below the label a textfield.
I tried to do this on xib file. I put a scrollView, and on this scrollView I put the tableview,the label and textfield. The problem is the view is not enough for all of them.
First of all,I want to see the tableview on screen and after I scroll the screen to see the label and textfield . I don't know how can I explain this...I mean that when app starts to see only the tableview on screen and after scroll to appear the label and textfield. Is it possible what I'm trying to do?
You have added those views (Tableview, label, textfield) in the nib file right?
Link all them to different variable to refer them.
set the frames through coding. like
tblView.frame = CGRectMake(0, 0, 320, 416);
label.frame = CGRectMake(60, 420, 200, 50);
textField.frame = CGRectMake(40, 480, 240, 31);
finally set
tblView.scrollEnabled = NO;
scrollView.contentSize = CGSizeMake(320,520);
UITableView is scrollable and i don't think that you need to take a UIScrollView for your task.You can add a label as a cell row textlable and a UITextField as an AccessoryType in your TableView.
Hope it would help.
There can be two approaches for your problem.
First approach :
No need of taking a UIScrollView. You can shorten your UITableView height and fit all three views on your screen.
Second approach :
Increase the content size of UIScrollView (increase Y parameter).
Yes it is 100% possible. You need to go through two types of tutorials and then you can integrate them for your own use.
You need to learn how to build customized table cell.
You need to learn to create table with multiple headers.
You don't need to use scrollview at all as it is already there in tableview. Or in simple words tableview is build on scrollview. So scrolling property is inherited.
Merge both techniques and you will have you custom tableview. It would not be easy but definitely you can achieve it.

Can't get label to show over background of grouped UITableView

I am trying to get a label to show over part of the grey-space in a grouped UITableView, but it only appears if the label is in a cell. I have tried adding the label as a subview of both the ViewController and the tableView and bringing it to front, but in either case it only shows over the white-space of a cell, not over the grey-space of the background. I know that I am a complete noob at Obj-C and iPhone dev and that this is a really stupid question, but I would really appreciate any help.
My code:
CGRect cgRct = CGRectMake(180, 20, 100, 50);
label = [[UILabel alloc] initWithFrame:cgRct];
label.text = #"Editting On";
label.textColor = [UIColor redColor];
label.hidden = TRUE;
//Display label
[tableView addSubview:label];
[tableView bringSubviewToFront:label];
There is probably a way to get this to work, but I'm betting that what you're trying to do is much simpler than you're making it. So, maybe explain what you want to do.
The first thing I see is you've set the hidden property to TRUE (you should be using YES, instead of TRUE by the way). If the label is hidden, you won't be able to see it, so either remove that line or change its parameter to NO.
Next, I should point out that you can add custom views for the table header and footer. You can even use custom views for section headers and footers. If what you want is a custom cell, then you'll want to either code that by hand or create a custom cell view in Interface Builder and then load it dynamically.
If what you are wanting, however, is to overlay the entire view with another view, then it will work to add the view to the top level controller's view--which is to say if you have a navigation based app, for example, you can access the navigation controller and add your view as a subview.
[[[self navigationController] view] addSubview:label];
HTH.

Editable table view cell

I am creating an application. I have to implement a bookmark feature, and adding one should be similar to this:
mmm http://img534.imageshack.us/img534/9859/schermafbeelding2010040a.png
I want editable UITableViewCells for text input. I was wondering if there is an easier way then embedding a UITextField into a UITableViewCell. And if not, can someone explain how I can use the UITextField inside it? Thanks
There isn't an easier way, you will have to subclass UITableViewCell and add in the UITextField. Here is a tutorial for using a UITextView should be about the same as a UITextField.
You can just add a textfield to the contentView of your cell, and an (X) button to the accessoryView of your cell. You could do that in a UITableView subclass which would let you add keep track of your added elements with instance variables or properties.
I found this helpful: Cocoa With Love: Easy custom UITableView drawing
Like this:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"myEditableCellId"];
label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
[cell.contentView addSubview:label];
You can use https://github.com/escoz/QuickDialog for simple cases like that. But you probably need to write your own custom cell if it's more complex.