表格单元子视图迭代未找到TextField [英] Table Cell SubView Iteration Not Finding TextField

查看:169
本文介绍了表格单元子视图迭代未找到TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表,其中每个单元格都包含文本标签和文本字段。我正在添加文本字段 [cell addSubview:passwordField]; ,从视觉角度看,它们出现并可编辑等等。

I've created a table where each cell holds both a text label and a text field. I'm adding the textfields as such [cell addSubview:passwordField]; and from a visual perspective they appear and are editable, etc....

当我尝试从文本字段中检索输入的值时,会出现问题。我遍历单元格并尝试抓取子视图(IE文本字段),但是,我的迭代只发现文本标签。

The problem arises when I attempt to retrieve the entered values from the textfields. I iterate over the cells and try to grab the subview (IE the textfield), however, my iteration only discovers the text label.

这是我正在使用的代码搜索:

Here is the code I'm using to search:

for(NSInteger i =0; i < [tableView numberOfRowsInSection:0]; i++){
   NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
   UIView* subView = [[cell.contentView subviews]lastObject]; // I've also tried object at index here

// Anything beyond this is just matching....

我采取的另一种方法是递归搜索子视图,但是,再次没有产生任何结果。

Another approach I took was recursively searching the subviews, but, again that yielded no results.

推荐答案

您已在单元格的子视图中添加了 textField

You have added your textField on subView of cell.

[cell addSubview:passwordField];

当您尝试在 cell.contentView

添加 textField 作为的子视图.cell.Contentview

[cell.contentView addSubview:passwordField];

以这种方式找到它 -

And find it in this way -

for(UIView *view in [cell.contentView subviews])
{
    if([view isKindOfClass:[UITextfield class]])
    {
       UITextField *textField = (UITextField *)view;
       NSLog(@"%@",textField.text);
    }
}

这篇关于表格单元子视图迭代未找到TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆