简体   繁体   English

获取值表单文本UITableView自定义单元格中的字段

[英]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 . 我创建了一个自定义的UITableViewCell ,该单元格有多个文本字段,现在我想访问这些UITextFields的字符串或数据。 I know i can get the cell on didSelectRowAtIndexPath , but i need to get the text on a "Save" method. 我知道我可以在didSelectRowAtIndexPath上获取单元格,但我需要在“保存”方法上获取文本。

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. 假设您有四个带有标签100的文本字段,依此类推至104.您将使用计数器显示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
    }

You can simply use viewWithTag to get your desired views. 您只需使用viewWithTag即可获得所需的视图。 Suppose you have one imageview with tag 100 and one text view with tag 200. 假设您有一个带有标签100的图像视图和一个带有标签200的文本视图。

UITableViewCell *cell = [mytableview cellForRowAtIndexPath:indexPath];

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

you need to create instance for each textField in your header file. 您需要为头文件中的每个textField创建实例。 Look at this sample demo 看看这个示例演示

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

You can access textfield's text property for getting the text value for a particular textfield 您可以访问textfield的text属性以获取特定文本字段的文本值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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