-1

我有很多文本字段静态放置在静态 TableViewcells 中。如何在不为每个文本字段创建引用出口的情况下访问其中的文本?我尝试过对单元格进行子视图。它不工作。有什么选择吗?

我试过这个。它不工作。

 - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [super tableView:tableView
                   cellForRowAtIndexPath:indexPath];
     for (UIView *subview in [cell subviews]){
        if([subview isKindOfClass:[UITextField class]]){
            UITextField *inputField = (UITextField *)subview;
            NSLog(@"%@",inputField.text);
        }
     }

    return cell;
}
4

3 回答 3

1

您可以使用 IBOutletCollection。这使您可以将它们全部连接到一个数组,您可以在其中通过索引访问它们,或使用 setValue:forKey: 一次解决它们。

于 2013-02-12T03:15:34.690 回答
0

您始终可以遍历单元格的每个子视图以找到UITextField. 就像是:

NSArray *subviews = [cell subviews];
for (view in subviews) {
    if([view isKindOfClass:[UITextField class]]) {
        //Do what you want with your UITextField
    }
}
于 2013-02-12T02:47:50.717 回答
0

跟着这些步骤:

  1. 设置tag为您的textField

    textField.tag = kTextFieldTag;
    
  2. subViewcell那个检索tag

    UITextField *refTextField = [cell viewWithTag:kTextFieldTag];
    

在这里,kTextFieldTag是恒定的。

于 2013-02-12T03:59:54.210 回答