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