简体   繁体   English

添加UITextField作为UITableViewCell的子视图在IOS 6中正常工作但在IOS 7中它不起作用?

[英]Added UITextField as a subview of UITableViewCell working fine in IOS 6 but in IOS 7 it is not working?

In my app i have a login form, where user enter one field password. 在我的应用程序中,我有一个登录表单,用户输入一个字段密码。 so i added a subveiw of UITextField in UITableViewCell during function call of cellForRowAtIndexPath . 所以我在cellForRowAtIndexPath函数调用期间在UITableViewCell中添加了一个UITextField视图。

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"loginViewCell"];

UITextField *editableTextField = nil;

// Done Some settings of editableTextField 

// Adding Subview 

[cell addSubview:editableTextField];

When user press login button i called a selector function name login 当用户按下登录按钮时,我调用了选择器功能名称登录

UITableViewCell *cell = [loginTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

// Then i travesed in the subviews of cell and from UITextField subview i extract password which was entered by user. //然后我在cellsubviews进行了切换,并从UITextField子视图中提取了用户输入的密码。

for (UIView *view in cell.subviews) {

    if ([view isKindOfClass:[UITextField class]]) {
        usernameField = (UITextField *)view;
                    break;
    }
}

This is working fine till IOS 7 这项工作正常,直到IOS 7

After searching on net i trace the problem that in IOS 7 Apple changes the view hierarchy of UITableViewCell and now there is additional class inserted UITableViewCellScrollView . 在网上搜索我跟踪问题,在IOS 7 Apple更改了UITableViewCell的视图层次结构,现在还有其他类插入UITableViewCellScrollView

I debug my code in my selector function login it is getting the same cell in which i added the subview i print the name of cell.subview is is showing UITableViewCellScrollView previously it was showing UITableViewCellcontentView (before ios7 ) 我在我的选择器函数登录中调试我的代码它正在获取相同的单元格,其中我添加了subview我打印了cell.subview的名称是显示UITableViewCellScrollView之前它显示的是UITableViewCellcontentView (在ios7之前)

How can I extract the password from subeview of UITableViewCell ? 如何从UITableViewCell subeview中提取密码?

The easiest way would be to subclass your UITableView cell and add a class property that is your UITextField. 最简单的方法是子类化UITableView单元格并添加一个类属性,即UITextField。 Then you could just call cell.textField.text instead of searching through the view hierarchy. 然后你可以调用cell.textField.text而不是搜索视图层次结构。

The second way would be to search recursively through the entire view hierarchy, not just a single layer of subviews. 第二种方法是递归搜索整个视图层次结构,而不仅仅是单层子视图。 (and you should be searching the cell.contentView anyway, bad things happen when you add views as subview's of the cell directly.) (而且你应该搜索cell.contentView,当你直接将视图作为单元的子视图添加时,会发生不好的事情。)

EDIT adding code for searching recursively through view hierarchy. 编辑添加代码以便通过视图层次结构递归搜索。 I do not recommend this method, I recommend subclassing UITableViewCell (it will make your like so much easier), but here you go. 我不推荐这种方法,我建议继承UITableViewCell(它会让你更容易),但是你走了。

You would call a function like 你可以调用类似的函数

UIView *yourFoundSubview = [self findTextFieldInCell:cell];

And that function would be defined: 该功能将被定义:

-(UIView*)findTextFieldInCell:(UIView*)input
{
    if([input isKindOfClass:[UITextField class]])
    {
        return input;
    }

    UIView *foundview;
    for(UIView *view in input.subviews)
    {
        foundview = [self findTextFieldInCell:view];
        if(foundview)
        {
            return foundview;
        }
    }

    return nil;
}

The cellForRowAtIndexPath: method is not the right place to add subviews but rather to manipulate them. cellForRowAtIndexPath:方法不是添加子视图而是操纵它们的正确位置。 You should really create a subclass of UITableViewCell having a @property UITextField which is added to the cell within the initWithStyle:reuseIdentifier: method. 你应该真正创建一个UITableViewCell的子类,它有一个@property UITextField ,它被添加到initWithStyle:reuseIdentifier:方法中的单元格中。

[self.contentView addSubview:self.passwordField];

Then you can access this particular UITextField with [cell passwordField] or whatever you want to call it. 然后,您可以使用[cell passwordField]或任何您想要调用它的方式访问此特定的UITextField

I believe you need to get the subviews of the UITableViewCellScrollView . 我相信你需要获得UITableViewCellScrollView的子视图。

Instead of adding the textfield to the cell, add the UITextField to cell.contentView , and look for the textField in the subviews of the cell.contextView . 不是将文本字段添加到单元格,而是将UITextField添加到cell.contentView ,并在cell.contextView的子视图中查找textField

I think what would really be best in the long run for your solution though, would be to create a custom UITableViewCell , and add the textField in there. 我认为从长远来看,对于您的解决方案来说,最好的方法是创建一个自定义的UITableViewCell ,并在其中添加textField You could directly access your textfield that way without having to loop through the subviews of the cell.contentView . 您可以直接访问文本字段,而无需循环遍历cell.contentView的子视图。

You could do 2 things: If you have only have 1 textfield for you entire viewcontroller, you define a property that holds a reference to your passwordTextField 你可以做两件事:如果整个viewcontroller只有一个textfield,你可以定义一个属性来保存对passwordTextField的引用

@property(strong, nonatomic) UITextField *passwordTextfield;

Or, if you have a textfield for each tableviewCell, you could define a collection of UITextFields 或者,如果每个tableviewCell都有一个文本字段,则可以定义UITextField的集合

@property(strong, nonatomic) NSMutableArray *textFields;

then you can reference your passwordTextField with: 那么您可以引用您的passwordTextField:

UITextField *passwordField = self.textFields[PASSWORD_ROW];

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

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