When using TableView, sometimes we need to add a button and a label to the cell in order to add a feature, and often the method in which the button is clicked needs to know the value stored in the label in the cell where the button resides.
In general we can use tag to do, but when table has a lot of lines, set tag is not so convenient, here I introduce another method.
We know that every view in iOS has a parent view, so can we get the cell using this method?
After finding the test found the Superview method can find the cell where the button resides
Write a method like this to the button.
-(void) Btn_onclick: (ID) sender{ *btn = (UIButton *) sender; NSLog (@ " [btn Superview] = ", [[Btn Superview]class]); NSLog (@ " [[[ btn Superview]superview]class] =%@", [[[Btn Superview]superview] class ]);}
The output is as follows
-42.203 superview[4720: 60b] [ BTN Superview] = from:42.646 superview[4720: 60b] [[[Btn Superview]superview]class] = UITableViewCell
That means it's fine. But after my test, I found that for my own writing inherited from the UITableViewCell class, (Here I am Songtableviewcell) need to make a minor change.
NSLog (@ " [btn Superview] = ", [[Btn Superview]class]); NSLog (@ " [[[ btn Superview]superview]class] =%@", [[[Btn Superview]superview] Class]); NSLog (@ " [[[ btn Superview]superview]superview]class] =%@"class] );
The output results are as follows:
the- the-Ten -: Wu:50.036music_player[4451: 60b] [btn superview] =Uitableviewcellcontentview the- the-Ten -: Wu:50.036music_player[4451: 60b] [[[Btn Superview]superview]class] =Uitableviewcellscrollview the- the-Ten -: Wu:50.036music_player[4451: 60b] [[[Btn Superview]superview]superview]class] = Songtableviewcell
PS: For the Viewwithtag method to say a word, this method can not look for the tag 0 control, even if its own button tag 0 can not be found, guess is with the system default space duplication.
IOS gets cell via button