2010-03-18 64 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); 
} 
相关问题