IOS gets cell via button

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.