Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nest a UITextField within a UITableViewCell?

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.

Calendar.app Title & Location action sheet

like image 542
John Topley Avatar asked Apr 02 '11 17:04

John Topley


2 Answers

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];
}
like image 152
3 revs, 2 users 98%user470763 Avatar answered Oct 26 '22 04:10

3 revs, 2 users 98%user470763


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.

like image 26
tJener Avatar answered Oct 26 '22 02:10

tJener