我有一個UITableView。每行右側有一個按鈕。現在問題是我點擊的任何按鈕,它返回indexPath.row的值爲0.但是,當我點擊一個按鈕,它應該返回我indexPath.row的相應值。我在iOS 7中工作。我在做什麼錯誤?按鈕動作UITableView indexPath.row值錯誤
下面是對細胞創建按鈕的代碼:
- (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;
下面是選擇方法的代碼:
- (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];
}
請參閱[這個答案](http://stackoverflow.com/a/5690329/457406)如何獲得的indexPath如果蘋果決定再次更改視圖層次結構,單元格將不會中斷。這就是爲什麼你的代碼無法工作,你的代碼在iOS6上沒問題,但是在iOS7中,蘋果在單元和它的contentView之間引入了一個滾動視圖。 –