iCoder86
iCoder86

Reputation: 1895

take text input from uitableviewcell

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.

Upvotes: 3

Views: 6520

Answers (3)

PgmFreek
PgmFreek

Reputation: 6402

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];
   }
}

Upvotes: 4

Amit Singh
Amit Singh

Reputation: 8383

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

Upvotes: 1

visakh7
visakh7

Reputation: 26390

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.

Upvotes: 0

Related Questions