簡體   English   中英

如何在自定義的tableViewCell中獲取UIButton的indexPath?

[英]How can I get the indexPath of UIButton in a customized tableViewCell?

我創建了一個tableViewCell,包括一個圖像,兩個文本標簽和一個uibutton。 該按鈕被分配給一個動作方法(例如viewButtonPused:sender)。

我習慣用tableView處理行選擇:didSelectRowAtIndexPath:所以我可以判斷選擇了哪一行。 但是使用uibutton及其動作方法......我怎么能說出來?

提前致謝。

如果按鈕的目標是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實例,即通過循環瀏覽超級視圖直到找到一個。

在與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
    }];

}

我知道這是一個舊線程但我發現這個方法最好,因為它沒有superView調用(因此當Apple使用新的os版本更改視圖層次結構時,它不受影響)並且不需要子類化或使用繁瑣的標記當重復使用單元格時,可能會搞砸。

- (void)buttonPressed:(UIButton *)sender {
    CGPoint location = [sender convertPoint:sender.center toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    NSLog(@"%@", indexPath);
}
- (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);
}

如果你直接在單元格上添加了元素(你不應該) -

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

如果您在單元格的contentView上添加了元素(這是建議的方式)

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

定義一個類變量int SelectedRow; didSelectRowAtIndexPath為SelectedRow = indexPath.row賦值給它;

在您的操作方法viewButtonPused:sender使用此SelectedRow變量

不是將按鈕添加為單元格的子視圖,而是將其設置為cell.accessoryView 然后使用tableView:accessoryButtonTappedForRowWithIndexPath:來完成當用戶點擊此按鈕時應該完成的事情。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM