How to get other control value from UITableViewCell?

Kapil Choubisa picture Kapil Choubisa · May 14, 2011 · Viewed 9.1k times · Source

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

Answer

Iphone_bharat picture Iphone_bharat · May 14, 2011

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