如何从UITableViewCell获取其他控件值? [英] How to get other control value from UITableViewCell?

查看:73
本文介绍了如何从UITableViewCell获取其他控件值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疑问,当我们在自定义tableViewCell时选择一行或点击按钮时,如何获取其他控件的值. 假设我有一个带有TextField,滑块和按钮的TableView单元格.按钮具有以下操作:

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];

现在我可以在其操作方法上找到按钮了.我需要在点击按钮时获取textField的值和滑块的值.

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.

请导航到获取UITableViewCell的子视图值的正确方法.

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

谢谢

推荐答案

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

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];

,然后在您的buttonTapped:方法中

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];
}

这篇关于如何从UITableViewCell获取其他控件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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