繁体   English   中英

在UITableView中的自定义单元格中引用UITextField

[英]Referencing UITextFields in a custom cell in a UITableView

我有一个静态UITableView ,有5个单元格如下:

自定义UITableViewCell

当用户选择顶行时,我需要执行一个将数据填充到第二行的方法。

在对单元格进行排队时,我添加了一个目标动作,如下所示:

[cell.upperTextField addTarget:self action:@selector(upperTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

我收到了有关此事件的通知。 如何引用第二个文本视图(找到CellForRowAtIndexPath )以将数据输入其中?

我无法使用didSelectRow因为我禁用了选择 - 用户只能使用提供的TextFields之一。

将标记值添加到UITextFields:

textField1.tag = 1;
textField1.tag = 2;

在upperTextFieldDidChange中:获取对textField的superview的引用,并按标记查找视图:

-(void)upperTextFieldDidChange:(id)sender
{
    UITextFields *tf = (UITextFields*)sender;
    int currentTag = tf.tag;
    // Do any validation if needed
    //Get reference to text field superview
    UIView *v = [tf superview];
    // Get reference to your second text field
    int tfTag = currentTag == 1 ? 2 : 1;
    UITextField *anotherTextField = (UITextField*)[v viewWithTag: tfTag];
    // Execute your method do other stuff here
}

希望这有帮助

使用自定义单元格并使用CellForRowAtIndexPath方法查找单元格对象,现在您可以轻松找到如下所示的文本字段。

MyCellView myCell = [myTableView cellForRowAtIndexPath:<your index path>];
myCell.firstTextField 

要么

myCell.secondTextField

如果要更改文本。 您可以轻松更改textField的文本。

如果认为通过继承UITableViewCell并在该单元格中处理内部更容易。 然后,您可以直接访问正确的文本字段。

创建一个.h和.m

@interface YourCell : UITableViewCell

然后在您的storyboard单元的interfacebuilder检查器部分中为该类命名。 然后在cellForRowAtIndexPath中调用单元格

YourCell *tempCell = (YourCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

暂无
暂无

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

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