I have a UITableViewCell
(Custom cell) in which i am creating some buttons and textfields and assigning tags to the buttons and textfields. But i couldn't get button title and textfield values on click on button.
In cellForRowAtIndexPath
`[((CustomCell *) cell).btn setTag:rowTag];
[((CustomCell *) cell).textField2 setTag:rowTag+1];`
-(IBAction)submitBtnAction:(UIControl *)sender
{
for (int i=0; i<[self->_dataArray count]; i++)
{
NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0];
NSLog(@"myIP.row %d",myIP.row);
UITableViewCell *cell = [tblView1 cellForRowAtIndexPath:myIP];
NSLog(@"tag %d",cell.tag);
UIButton *btn = (UIButton *)[cell.contentView viewWithTag:i];
NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag);
UITextField *tf = (UITextField *)[cell.contentView viewWithTag:i+1];
NSLog(@"tf text %@, tag %d",tf.text,btn.tag);
}
}
I'm getting error like this
-[UITableViewCellContentView titleLabel]: unrecognized selector sent to instance 0x71844e0
2013-07-17 13:48:29.998 Text[1271:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView titleLabel]: unrecognized selector sent to instance 0x71844e0'
I think you can directly access the btn
and textField2
properties of your cell, once you get it from cellForRowAtIndexPath:
. Assuming you are creating and returning instance of CustomCell
, just typecast it to CustomCell
instead of UITableviewCell
. See modified code below
-(IBAction)submitBtnAction:(UIControl *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0];
//Type cast it to CustomCell
CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:myIP];
UIButton *btn = cell.btn;
NSLog(@"btn text %@, tag %d",btn.titleLabel.text,btn.tag);
UITextField *tf = cell.textField2;
NSLog(@"tf text %@, tag %d",tf.text,btn.tag);
}
Hope that helps!
Simple way to do it:
-(IBAction)submitBtnAction:(UIControl *)sender
{
UIButton *senderButton = (UIButton *)sender;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:i inSection:0];
CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:myIP];
NSLog(@"cell.textField -tag :%d",cell.textField2.tag);
NSLog(@"cell.btn -tag :%d",cell.btn.tag);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With