IOS gets its ordinal after adding a button click event for the custom TableView

Source: Internet
Author: User

In custom TableView, after adding a button click event for a cell, how do I get its corresponding ordinal number?

1. Create TableView:

First create a member variable:

@interface Mycameraviewcontroller () <UITableViewDelegate,UITableViewDataSource>

{

UITableView *_tableview;

} @end

Initialize in Viewdidload

_tableview = [[UITableView alloc] Initwithframe:cgrectmake (0, +, +, [UIScreen mainscreen].bounds.size.height-65) Style:uitableviewstyleplain];
_tableview.delegate = self;
_tableview.datasource = self;
_tableview.backgroundcolor = [Uicolor Clearcolor];
[Self.view Addsubview:_tableview];

2, the implementation of the proxy method must be implemented

#pragma mark-table View data source

Number of groups
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
Return the number of sections.
return 1;
}
Number of cells
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
Return the number of the rows in the section.
return 10;
}

3. Create cell

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
    static NSString *cellidentifier = @ "Cell";
   UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
   //Configure the cell ...
        if (nil = = cell) {
        cell = [[UITableViewCell alloc] Initwithframe:cgrectmake (0, 0, 320, 44)];
Cell.textLable.text = @ "Hello, hello";

UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
CGRect frame = CGRectMake (0.0, 0.0, 30, 24);
Button.frame = frame;
[Button Setimage:image Forstate:uicontrolstatenormal]; You can set a picture for a button
Button.backgroundcolor = [Uicolor Clearcolor];
[Button addtarget:self action: @selector (accessorybuttontappedaction:) forControlEvents: UIControlEventTouchUpInside];

Assign the button to the cell property Accessoryview
Cell. Accessoryview = button;
}

return cell;

}

//Implement button's Click event
-(void) Accessorybuttontappedaction: (ID) sender
{
    UIButton *button = (UIButton *) sender;
    uitableviewcell  *cell;
    if (iOS7) {
        cell = (UITableViewCell *) Button.superview.superview;
   }else
    {
        cell = ( UITableViewCell *) Button.superview;
   }  
    int row = [_tableview indexpathforcell:cell].row; Row is the ordinal
}

where the button is located

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.