I have a UITableView. There is a button in the right side on each row. Now the problem is any button i tap, it returns me the value of indexPath.row is 0. But when i tap a button it should return me the corresponding value of indexPath.row. I am working in iOS 7. Where is the mistake i am doing?
Here is the code for creating buttons on cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger index = [indexPath row];
UISegmentedControl *renameButton = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
renameButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Rename"]];
[renameButton setTag:6];
[renameButton addTarget:self action:@selector(renameButtonClicked:) forControlEvents:UIControlEventValueChanged];
[renameButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[renameButton setMomentary:YES];
[renameButton setTintColor:[UIColor lightGrayColor]];
[cell.contentView addSubview:renameButton];
[renameButton release];
return cell;
Here is the code for the selector method:
- (void)renameButtonClicked:(id)sender {
isInEditMode = !isInEditMode;
[self setToolbarUserInterface];
[self setTableViewUserInterface];
while ([selectedItems count] > 0) {
[selectedItems removeLastObject];
}
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
int index = [[self.tableView indexPathForCell:cell] row];
NSLog(@"Index value : %d",index);
NSString *newPath = [path stringByAppendingPathComponent:[items objectAtIndex:index]];
TextInputViewController *textInput = [[TextInputViewController alloc] init];
[textInput setPath:newPath Parent:parent andType:5];
[self.navigationController pushViewController:textInput animated:YES];
}