Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Values form text Fields in UITableView Custom Cell

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.

like image 875
Zubair Avatar asked Jul 26 '12 09:07

Zubair


2 Answers

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
    }
like image 178
junaidsidhu Avatar answered Oct 14 '22 02:10

junaidsidhu


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];
like image 27
Nazar Hussain Avatar answered Oct 14 '22 03:10

Nazar Hussain