Get Values form text Fields in UITableView Custom Cell

17,193

Solution 1

Suppose you have four text fields with tags 100 and so on to 104. You will you Counter that shows how many cells you have in tableview.

         

for (int i=0; iLessThanCounter; i++) {
            
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0];
                
     UITableViewCell *cell = [mytableview cellForRowAtIndexPath:indexPath];
                
     for (UIView *view in  cell.contentView.subviews){                
                    
           if ([view isKindOfClass:[UITextField class]]){
                        
                  UITextField* txtField = (UITextField *)view;
                        
                  if (txtField.tag == 100) {
                         NSLog(@"TextField.tag:%u and Data %@", txtField.tag, txtField.text);
                  }
                  if (txtField.tag == 101) {
                        NSLog(@"TextField.tag:%u and Data %@", txtField.tag, txtField.text);
                  }
                  if (txtField.tag == 102) {
                        NSLog(@"TextField.tag:%u and Data %@", txtField.tag, txtField.text);
                  }
                  if (txtField.tag == 103) {
                       NSLog(@"TextField.tag:%u and Data %@", txtField.tag, txtField.text);
                  }
                  if (txtField.tag == 104) {
                       NSLog(@"TextField.tag:%u and Data %@", txtField.tag, txtField.text);

                  } // End of isKindofClass 
              } // End of Cell Sub View
         }// Counter Loop
    }

Solution 2

You can simply use viewWithTag to get your desired views. Suppose you have one imageview with tag 100 and one text view with tag 200.

UITableViewCell *cell = [mytableview cellForRowAtIndexPath:indexPath];

UIImageView *getImageView = (UIImageView*)[cell.contentView viewWithTag:100];
UITextField *getTextView = (UITextField*)[cell.contentView viewWithTag:200];

Solution 3

you need to create instance for each textField in your header file. Look at this sample demo

http://windrealm.org/tutorials/uitableview_uitextfield_form.php

You can access textfield's text property for getting the text value for a particular textfield

Share:
17,193
Zubair
Author by

Zubair

Updated on June 27, 2022

Comments

  • Zubair
    Zubair almost 2 years

    I have Created a custom UITableViewCell, the cell has multiple text fields, now i want to access the strings or data in these UITextFields. I know i can get the cell on didSelectRowAtIndexPath, but i need to get the text on a "Save" method.

  • Zubair
    Zubair almost 12 years
    I have multiple textFields in a cell, & there are also multiple cells,
  • Rahul Vyas
    Rahul Vyas almost 12 years
    so you can keep references for all of them
  • Rahul Vyas
    Rahul Vyas almost 12 years
    one more solution is above solution from Muhammad Junaid Sidhu which might do the work
  • manileo86
    manileo86 almost 12 years
    This way will solve the purpose. But best way is to do it through delegates.
  • Nazar Hussain
    Nazar Hussain almost 12 years
    If you do have large number of views inside the cell this code will be ambiguous. You are conterminously checking tag on if conditions, even you are not using else if or the best tool for this scenario switch statement. And if you directly get the view for a specific tag then why do you iterate with a loop?
  • Siva
    Siva over 10 years
    @manileo86 help me i have faced same problem dropbox.com/s/lowbgnqzk05ej8d/Alert.zip
  • Siva
    Siva over 10 years
    help me i have same logic
  • Abhishek
    Abhishek about 8 years
    perfect only for the visible cells in the tableview...but one cannot get data for the cells scrolled up.