Custom Accessoryview of UITableViewCell and access events

Source: Internet
Author: User

1. Add a button to the cell as a Accessoryview

When there is only one section, when the cell content is set, the Tag=cell_index of the button in the cell can be set separately, and the cell belongs to the tag of the button.

When you have more than one section, you can loop through the parent view of the button until you get to the cell that belongs to it

e.g.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath{
uitableviewcell* cell = nil;
cell = [TableView dequeuereusablecellwithidentifier:mycellidentifier Forindexpath:indexpath];

            .....
           uibutton *button = [UIButton Buttonwithtype:uibuttontypesystem];
           button.frame = CGRectMake (0.0f, 0.0f, 150.0f, 25.0f);
           [button settitle:@ "Actionbutton"   forstate: UIControlStateNormal];
           [button addtarget:self action: @selector (performaction:)   FORCONTROLEVENTS:UICONTROLEVENTTOUCHUPINSIDE]; &NBSP
            Cell.accessoryview = button;
           return cell;
   }  

-(void) Performaction: (UIButton *) paramsender{
UITableViewCell Parentcell = (UITableViewCell *) [Self Superviewoftype:[uitableviewcell class] forview:paramsender]; Get the cell where the button is located
。。。。。
}

Loops the parent view until it gets its cell

 -(UIView *) Superviewoftype: (Class) Paramsuperviewclass  forview: (UIView *) paramview{
        if (Paramview.superview!= nil) {
                 if ([param View.superview Iskindofclass:paramsuperviewclass] {
                  return paramview.superview;
       } else {
            return [self SUPERVIEWOFTYPE:PARAMSU perviewclass forview:paramview.superview];
       }
   }
    return nil;   
}      &NBSP

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.