如何从UITableViewCell获取其他控件值?

发布于 2024-11-07 20:27:26 字数 423 浏览 12 评论 0原文

我有一个疑问,当我们在自定义 tableViewCell 时选择一行或点击按钮时如何获取其他控件的值。 假设我有一个带有 TextField、滑块和按钮的 TableView Cell。按钮的操作使用:

btn.tag = indexPath.row;
[btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

现在我可以在其操作方法上获取按钮。我需要在点击按钮时获取文本字段的值和滑块的值。

一个选项是我创建一个数组并将这两个控件的值存储在该数组中并对其进行管理。但我认为这不是正确的管理方法。

请引导我找到获取 UITableViewCell 子视图值的正确方法。

谢谢

I have one doubt that how to get other control's value when we select a row or tap on button while having custom tableViewCell.
Suppose I have a TableView Cell with a TextField, a Slider and button. Button has action using:

btn.tag = indexPath.row;
[btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

Now I can get button on its action method. I need to get textField's value and slider's value on button tap.

The one option is I create a array and store values for these two control in that array and manage this. But I don't think this is correct way to manage it.

Please navigate me to the currect way to get value of UITableViewCell's subview's value.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-11-14 20:27:26

创建文本字段和滑块时,给它们一个常量标签,然后添加到单元格的 contentView 中

textfield.tag= TEXTFIELDTAG; //TEXTFIELDTAG = 100 or any other constant
[cell.contentView addSubview:textfield];

,然后添加到您的 buttonTapped: 方法中

-(void)buttonTapped:(id)sender
{

    int row = ((UIButton*)sender).tag;
    NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0]; // in case this row in in your first section
    UITableViewCell* cell = [table cellForRowAtIndexPath:indexPath];
    UITextField* textfield = [cell.contentView viewWithTag:TEXTFIELDTAG];
}

While creating your textfield and slider, give them a constant tag and then add to cell's contentView

textfield.tag= TEXTFIELDTAG; //TEXTFIELDTAG = 100 or any other constant
[cell.contentView addSubview:textfield];

and then in your buttonTapped: method

-(void)buttonTapped:(id)sender
{

    int row = ((UIButton*)sender).tag;
    NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0]; // in case this row in in your first section
    UITableViewCell* cell = [table cellForRowAtIndexPath:indexPath];
    UITextField* textfield = [cell.contentView viewWithTag:TEXTFIELDTAG];
}
仅冇旳回忆 2024-11-14 20:27:26

每当您为 UITableViewCell 创建子视图(按钮、文本字段等)时,请不要忘记标记它们。

myTextField.tag = 0;
myTextLabel.tag = 1; 
[cell addSubview:myTextField];
[cell addSubview:myTextLabel];

标记后,您可以使用以下方法访问它们。

- (UIView *)viewWithTag:(NSInteger)tag

当您选择时,您可以使用UIViewViewWithTag函数获取UITableViewCell的子视图。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexPath];
        UITextField* myTextField = [cell  viewWithTag:0];
        UILabel* myTextLabel     = [cell  viewWithTag:1];
    }

Whenever you create subviews (button , text field etc. ) for your UITableViewCell don't forget to tagged them.

myTextField.tag = 0;
myTextLabel.tag = 1; 
[cell addSubview:myTextField];
[cell addSubview:myTextLabel];

Once you tagged, you could access them using below method.

- (UIView *)viewWithTag:(NSInteger)tag

When you select,you could get the subviews of your UITableViewCell's by using ViewWithTag function of UIView.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexPath];
        UITextField* myTextField = [cell  viewWithTag:0];
        UILabel* myTextLabel     = [cell  viewWithTag:1];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文