2014-01-31 25 views
1

我有一個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]; 
} 
+0

請參閱[這個答案](http://stackoverflow.com/a/5690329/457406)如何獲得的indexPath如果蘋果決定再次更改視圖層次結構,單元格將不會中斷。這就是爲什麼你的代碼無法工作,你的代碼在iOS6上沒問題,但是在iOS7中,蘋果在單元和它的contentView之間引入了一個滾動視圖。 –

回答

0

你不通過調用得到的UITableViewCell:

UITableViewCell *cell = (UITableViewCell *)[[button superview] superview]; 

嘗試添加一個超級景觀訪問單元格:

UITableViewCell *cell = (UITableViewCell *)[[[button superview] superview] superview]; 

結構上表視圖按鈕佈局是:

UITableViewCellContentView - [sender superview] 
UITableViewCellScrollView - [[sender superview] superview] 
UITableViewCell - [[[sender superview] superview] superview] 

希望這有助於

+0

非常感謝!有用。 – John

+0

準備再次更改iOS8的「superview」調用次數。;-) **嚴重的是,不要使用依賴於來自其他框架的視圖視圖層次結構的代碼**。 –

+0

@MatthiasBauch我沒有說過這是做這件事的好方法。問題是關於這個例子中的錯誤,我只是指出了造成這個錯誤的原因。 – Greg

0

使用相關聯的對象。這應該工作:

- (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]; 

    objc_setAssociatedObject(renameButton, "cell", cell, OBJC_ASSOCIATION_ASSIGN); 

    [renameButton release]; 

return cell; 

選擇方法:

- (void)renameButtonClicked:(id)sender { 

isInEditMode = !isInEditMode; 
[self setToolbarUserInterface]; 
[self setTableViewUserInterface]; 

while ([selectedItems count] > 0) { 
    [selectedItems removeLastObject]; 
} 

UITableViewCell *cell = objc_getAssociatedObject(sender, "cell"); 
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]; 
} 

而且不要忘記導入runtime.h

#import <objc/runtime.h> 
0

我的做法是這樣的:

  1. 創建一個自定義按鈕類,它依賴於用戶繼承一個類lement你使用的是觸發一個動作,在你的情況下它是UISegmentedControl
  2. 然後添加一個屬性以保存對其索引路徑的引用。
  3. 當您創建您的UISegmentedControl設置其索引路徑。
  4. 在你的操作方法中,你可以簡單地得到它的索引路徑。

我認爲這是更方便的方式來做你想做的事情,因爲比方說,你想改變你的單元格的視圖層次結構。這是一個很好的機會,你的代碼需要改變。另外,獲取UIKit元素的第三個超級視圖永遠不是一個好主意。

-1

得到indexpath在這種情況下將是清晰的方式:

NSIndexPath *index = [myTableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]]; 
+0

這只是提問者在一行而不是三行中使用的代碼。 –