【问题标题】:Accessing textField embedded in cell访问嵌入在单元格中的 textField
【发布时间】:2012-12-24 04:06:58
【问题描述】:

我创建了一个表格,该表格可以动态创建自定义为包含文本字段的单元格。当我运行程序时,我可以在 textFields 中输入文本。但是,在退出程序/切换到不同的视图控制器之前,我无法收集输入到其中的文本。您能否建议我应该怎么做才能提取用户输入的文本。

我了解我可以使用以下代码访问单元格...

for (int section = 1; section < [self.tableView numberOfSections]; section++) // section 0: profile picture
{
    for(int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++)
    {
        NSLog(@"section = %d, row = %d", section, row);
        NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell *tempCell = [self tableView:self.tableView cellForRowAtIndexPath:tempIndexPath];
//            NSLog(@"tempCell = %@", tempCell);

    }
}

但我无法提取其中包含的文本。

我还提到了:Accessing UITextField in a custom UITableViewCell。但我正在寻找更清洁的解决方案。

谢谢!

【问题讨论】:

  • 您是否尝试过在自定义单元格类中使用 @ 属性使用 getter setter 来制作文本字段,并使用 tempcell.yourtextfield.text 访问这些内容。

标签: ios uitableview uitextfield uitextfielddelegate


【解决方案1】:

您引用的链接与您需要做的非常接近,但是有更好的方法来获取 indexPath。

在开始使用 iOS 编程时,一个常见的误解是您需要在需要数据时(例如当用户点击“提交”时)获取文本字段的所有值。问题,尤其是当它们在表格中时,文本字段并不总是可用的。如果单元格不在屏幕上,它很可能不存在,或者它已在表格的不同行中重复使用。文本字段是视图,应该显示数据,而不是作为存储数据的模型。

所以,您需要做的第一件事就是让您的视图控制器符合UITextFieldDelegate 协议,并在创建文本字段时将其设置为您的视图控制器:

您的 .h 文件(&lt;UITextFieldDelegate&gt; 是重要部分):

@interface YourViewController : UIViewController <UITextFieldDelegate>

创建文本字段时:

myNewTextfield.delegate = self;

这会告诉文本字段通知您对它​​的重要更改。现在,您只需要创建文本字段委托方法,该方法在完成编辑后立即调用文本字段并等待它被调用,以便您可以存储文本:

- (void) textFieldDidEndEditing:(UITextField *)textField {
    // If you need the index path of the table view cell which contains the text field in order to know how to store it, use:
    CGRect position = [self convertRect:textField.frame toView:self.tableView];
    NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:position];
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0];

    // Save the contents of the text field somewhere so that you have it later when you need it:
    something = textField.text;
}

【讨论】:

  • 当文本字段存在于单元格类中时,如何将文本字段委托设置为 self?单元类无法访问接口,因为它在 UIViewController 中,而不是在单元类中。
  • @JozemiteApps 这就是为什么我建议让视图控制器成为文本字段的代表......
  • 你的意思是:cell.myNewTextField.delegate = selfsince 控制器中不存在文本字段。
【解决方案2】:

This 教程对我很有帮助。你可以通过标签引用任何你需要的对象。

在情节提要中拖动 UIImageViewUITextField 等并将标签设置为 100(无论您想要什么),然后在您的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中使用标签来引用它。

这是你可以做的,只要记住在故事板中设置标签:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

 UITextField *tField = (UITextField *)[cell viewWithTag:100];

return cell;
 }

【讨论】:

    【解决方案3】:

    这是我的 Swift 4 解决方案,因为我遇到了同样的问题。

    我也在单元格中添加了文本字段,并且表格是动态创建的,因此表格视图控制器无法直接访问文本字段。

    首先,我创建了一个协议,允许表格视图控制器保存单元格文本字段的输入值的数据。

    protocol SMTextFieldRowCellProtocol: class {
        var dataValues: [String: String] { get set }
        func updateDataValues(with data: [String: String])
    }
    

    在单元格的自定义类中,添加以下委托属性:

    var delegate: SMTextFieldRowCellProtocol?
    

    仍在单元格的类中,使其符合UITextFieldDelegate,在awakeFromNib() 中将文本字段的委托设置为self。

    textField.delegate = self
    

    然后在单元格的类中,添加UITextFieldDelegate的方法,使其调用updateDataValues方法。我使我的键等于单元格的标题标签,所以所有键都是唯一的。

    func textFieldDidEndEditing(_ textField: UITextField) {
        delegate?.updateDataValues(with: [titleLabel.text!: textField.text!])
    }
    

    上述方法会更新表格视图控制器的字典。几乎完成了,但是每当您在表格视图的 cellForRow 方法中创建行时,请设置单元格的委托:

    cell.delegate = self
    

    最后,将协议添加到类中并遵守它:

    class SMEditProfileTVC: UITableViewController, SMTextFieldRowCellProtocol {
        var dataValues: [String: String] = [String: String]()
        func updateDataValues(with data: [String: String]) {
            for key in data.keys {
                dataValues[key] = data[key]
            }
        }
    }
    

    所以当你完成编辑单元格的文本时,它会调用表格视图控制器的updateDataValues 方法并将数据保存到dataValue 的字典中。现在,这将使数据在保存等时或在视图控制器中完成时更容易传输。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多