简体   繁体   中英

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标题和位置操作表

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM