3

i am developing an application where i need to take text input from user using sectioned table view. i had never use table view for taking text input from user. please have a look at the image for batter understanding.

enter image description here

i tried this In-place editing of text in UITableViewCell?

but this is not full filling what i wanted to.

when i use following code i get output which is not similar to above image.

UITextField *txtField=[[UITextField alloc]initWithFrame:CGRectMake(45, 0, cell.frame.size.width, cell.frame.size.height)];
    txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
    txtField.autoresizesSubviews=YES;
    [txtField setBorderStyle:UITextBorderStyleRoundedRect];
    [txtField setPlaceholder:@"Type Data Here"];

    if (cell == nil)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [cell addSubview:txtField];

enter image description here

see the answer of question 3 and 5.

Thanks.

Community
  • 1
  • 1
iCoder86
  • 1,874
  • 6
  • 26
  • 46
  • Why is the link you posted not fulfilling? Please, explain a bit more in detail what you trying to achieve... – sergio May 26 '11 at 12:53
  • @sergio : Hi, when i use the code given in link i used for solving my query, i dont see the behaviour same to as shown in image i posted in my question. that's what my concern to post the question. – iCoder86 May 27 '11 at 07:29
  • can you explain what are the difference in the behaviour? Could you post also the exact code you are using for `tableView:cellForRowAtIndexPath`, this would help a lot... – sergio May 27 '11 at 07:36

3 Answers3

4

you need to add UITextField to content view Corresponding cell, where you want the Text field. You select the cell you want by looking in to the cells indexpath.section and indexpath.row in cellForRowAtIndexPath:

Example:

if(indexPath.section == CELL_SECTION) {
    if(indexpath.row == CELL_ROW_INDEX) {
      UITextField *txtField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 320, 39)];
      txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
      txtField.autoresizesSubviews=YES;
      txtField.layer.cornerRadius=10.0;
      [txtField setBorderStyle:UITextBorderStyleRoundedRect];
      [txtField setPlaceholder:@"Type Data Here"];
      [cell.contentView addSubView:textField];
   }
}
bilby91
  • 904
  • 1
  • 8
  • 20
PgmFreek
  • 6,374
  • 3
  • 36
  • 47
  • @iPhonePrg : Hi i used the same code from what i tried first, but the issue with this code is it adds simple text field, its behavior is not similar to what i suggest in my question. – iCoder86 May 27 '11 at 05:11
  • what does this line do? txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight; – Henley Jul 29 '11 at 18:49
  • 1
    Last line should be `[cell.contentView addSubview:txtField];` – ITO Yosei Nov 11 '13 at 07:25
1

First thing first

  1. you need to make a subClass of UITableViewCell.

  2. Ofcoures that class will have a TextField(TF) as a property to access that TF

  3. set tag for that TF while initialising it.

  4. in the delegate method tableView:celForRowAtIndexPath: get that TF by tag use method tf = [cell viewWithTag:YOUR_TAG];

  5. Now you can do what ever you want.

  6. Now suppose you want the value outside this method then there are two way around it

    a. make a global variable and store value in it

    b. First find the cell by method of tableView by cellforRowAtIndexPath and repeate the step 4 and 5

Praveen S
  • 10,355
  • 2
  • 43
  • 69
Amit Singh
  • 8,383
  • 4
  • 28
  • 31
0

You need a custom UITableViewCell with a UITextfield in it. You can probably set a tag for the textField for each cell to identify them. You can also implement the UITextFieldDelegate if you want some validation on text or something. You can then get the value for the textfield based on the tag.

visakh7
  • 26,380
  • 8
  • 55
  • 69