iphone - Get row number of UITableView in IOS -


this question has answer here:

in code have table view many sections sorted alphabetically. have checkboxes(uibutton) in table rows , need check them according respective status entered in row. had done

checkbutton.tag = indexpath.row;  

but when table scrolls , section changed , status of previous section in checkbox method. please me on this.

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {    checkbutton = [uibutton buttonwithtype:uibuttontypecustom];    [checkbutton addtarget:self action:@selector(checkboxaction:)      forcontrolevents:uicontroleventtouchupinside];    checkbutton.tag=indexpath.row;    //some other stuff    [cell.contentview addsubview:checkbutton]; } 

and checkboxaction

-(void)checkboxaction:(uibutton *)btn {     tag = btn.tag;     nsdictionary *tagdict =[statusarray objectatindex:tag];  } 

now, problem indexpath.row 0 whenever section changed, can't able access exact row number in checkboxaction.

use getting current row tap.

 - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {  checkbutton = [[uibutton alloc] init];  [checkbutton addtarget:self action:@selector(checkboxaction:)     forcontrolevents:uicontroleventtouchupinside]; checkbutton.tag=indexpath.row; [cell addsubview:checkbutton]; }   -(void)checkboxaction:(id)sender {   // cast sender uibutton uibutton *button = (uibutton *)sender;  // find point in superview cgpoint pointinsuperview = [button.superview convertpoint:button.center toview:self.tableview];  // infer index path nsindexpath *indexpath = [self.tableview indexpathforrowatpoint:pointinsuperview];  // log console nslog(@"selected row : %d", indexpath.row); } 

Comments