2010-03-18 36 views
4

我創建了一個tableViewCell,包含一個圖片,兩個文本標籤和一個uibutton。 該按鈕分配給一個操作方法(例如viewButtonPused:sender)。如何在定製的tableViewCell中獲取UIButton的indexPath?

我用來處理tableView:didSelectRowAtIndexPath行選擇:所以我可以知道哪一行被選中。但是隨着uibutton及其行動方法....我怎麼知道?

在此先感謝。

回答

4

定義與Cell的原型關聯的類的委託。

// MyCell.h

@protocol MyCellDelegate 
- (void)buttonTappedOnCell:(MyCell *)cell; 
@end 

@interface MyCell : UITableViewCell 
@property (nonatomic, weak) id <MyCellDelegate> delegate; 
@end 

// MyCell.m

@implementation MyCell 

- (void)buttonTapped:(id)sender { 
     [self.delegate buttonTappedOnCell:self]; 
    } 
} 
@end 

現在去你想Cell的委託類。這可能是一個UITableView子類。在cellForRowAtIndexPath方法中,確保將Cell的委託賦給self。然後執行協議中指定的方法。

- (void)buttonTappedOnCell:(MyCell *)cell { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    int row = indexPath.row; 
} 

或者,如果你希望有一個塊爲基礎的方法:

// MyCell.h

typdef void(^CellButtonTappedBlock)(MyCell *cell); 

@interface MyCell : UITableViewCell 
@property (nonatomic, copy) CellButtonTappedBlock buttonTappedBlock; 
@end 

然後在您的tableView的DataSource:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyCell *cell = .... 
    __weak typeof(self) weakSelf = self; 
    [cell setButtonTappedBlock:^(MyCell *cell) { 
     NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:cell]; 
     // Do stuff with the indexPath 
    }]; 

} 
+0

謝謝。這個答案非常有幫助。 – 2010-03-18 15:06:30

+0

但是我們怎麼能通過節? – 2014-01-07 10:08:13

+0

@ShreeshGarg我已經用適當的方式更新了我的答案。 – jamone 2014-02-04 16:47:10

0

定義一個類變量int SelectedRow;裏面的didSelectRowAtIndexPath賦值給它像 SelectedRow = indexPath.row;

使用在側動作方法viewButtonPused:sender

+0

謝謝,但didSelectRowAtIndexPath不會被調用,除非我觸摸該行而不僅僅是按鈕。 – 2010-03-18 10:19:57

17

如果按鈕的目標是UIViewController/UITableViewController或維持到UITableView實例的引用任何其他對象,這將很好地做:

- (void)viewButtonPushed:(id)sender { 
    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = button.superview; // adjust according to your UITableViewCell-subclass' view hierarchy 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    // use your NSIndexPath here 
} 

使用這種方法可以讓你避免額外的實例變量和將正常工作的情況下,您有多個部分。不過,您需要有一種方法來訪問UITableView實例。

編輯:有人在下面的評論中指出,這種做法在iOS的7分手如果你仍然有興趣使用這種方法了標籤,一定要找到UITableViewCell實例正確,通過循環,即直到你找到一個超級視圖。

+0

哦,我喜歡那種使用標籤的方式。 – jamone 2010-03-18 14:32:23

+0

做這個更好的方法有更好的方法。我正在尋找一些幫助,因爲我有一個按鈕,它只是刪除表格行(因此indexPath.row被更改,但標籤保持不變!)。 +1 – a1phanumeric 2011-05-19 11:24:46

+0

標籤解決方案很好,但是當你在tableview中有多個部分時,這是完美的 – jfisk 2012-11-18 23:57:06

0

我現在使用的另一種方法是繼承UIButton並添加一個NSIndexPath類型的屬性。 在cellForRowAtIndexPath我分配indexPath的值及其在action方法中的可用值。

2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ... 
    [cell.customCellButton addTarget:self action:@selector(customCellButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.customCellButton setTag:indexPath.row]; 
} 

- (void)customCellButtonTapped:(id)sender { 
    UIButton *button = (UIButton *)sender; 
    NSLog(@"indexPath.row: %d", button.tag); 
} 
1

如果你直接加在細胞本身的元素(你不應該) -

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[sender superview] ]; 

對細胞的內容查看如果你已經添加的元素(這是建議的方法)

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview] ]; 
3

我知道這是一箇舊的線程,但我覺得這個方法最好,因爲它是免費的superView調用(因此,當蘋果改變視圖層次結構與新的os版本不受影響),並不需要e子類化或使用繁瑣的標籤,當細胞被重新使用時可能會變得混亂。

- (void)buttonPressed:(UIButton *)sender { 
    CGPoint location = [sender convertPoint:sender.center toView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
    NSLog(@"%@", indexPath); 
} 
相關問題