IOS/Objective-C:在自定义表视图单元格中检测按钮按下



对于提要视图控制器,我有一个带有一些自定义单元格的表视图,这些单元格具有类似、注释和共享按钮。我想根据按下的按钮执行不同的操作。

我最初的想法是简单地将故事板中自定义单元格中的按钮连接到自定义单元格的操作方法。然而,我对自定义cell.m文件中的方法如何与视图控制器中的TableView交互感到困惑。

- (IBAction)likeButtonPressed:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
//do something
}

有人能澄清一个单元格中的按钮按钮是否可以连接到故事板中,以及这是如何与视图控制器上的委托方法、单元格在dexpath中的行和didselected在dexpath上的行一起进行的吗?

或者,如果这不是正确的方式,我们将感谢任何关于更好方式的建议。提前感谢您的任何建议。

不用delegatetags,只需使用blocks即可。Blocks比推荐的delegate模式更易于使用。

Swift中,你也会看到closures (blocks in Objective-C)比任何其他模式都得到了广泛的使用。

示例:

1.UITableViewDataSource方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.likeButtonTapHandler = ^{
NSLog(@"Like Button Tapped");
//ADD YOUR CODE HERE
};
cell.commentButtonTapHandler = ^{
NSLog(@"Comment Button Tapped");
//ADD YOUR CODE HERE
};
cell.shareButtonTapHandler = ^{
NSLog(@"Share Button Tapped");
//ADD YOUR CODE HERE
};
return cell;
}

2.自定义UITableView单元格

@interface TableViewCell : UITableViewCell
@property (nonatomic, copy) void(^likeButtonTapHandler)(void);
@property (nonatomic, copy) void(^commentButtonTapHandler)(void);
@property (nonatomic, copy) void(^shareButtonTapHandler)(void);
@end
@implementation TableViewCell
- (IBAction)likeButtonTapped:(UIButton *)sender
{
self.likeButtonTapHandler();
}
- (IBAction)commentButtonTapped:(UIButton *)sender
{
self.commentButtonTapHandler();
}
- (IBAction)shareButtonTapped:(UIButton *)sender
{
self.shareButtonTapHandler();
}

您可以通过两种方式来实现。最好是第一个要记住Objective-C设计模式。

  • 使用委托模式

    您可以向视图控制器发回一个调用,传递Button id(可以是标记或枚举)和单元格对象,并在那里处理这些内容。

  • 根据indexPath使用标签

    例如,如果你只有行而没有列,那么你可以根据按钮的行号和按钮类型分配按钮标签,如果你有行和节,你可以根据行和节的编号分配标签,然后为CellForRowAtIndexPath中的按钮分配选择器,然后你可以通过解码sender的标签来获得行号

现在,扩展第一种方法,无需获取按钮的位置。只需调用将在ViewControler中观察到的按钮的委托方法。

在自定义单元格类中。

@protocol YourCellButtonsDelegate <NSObject>
@optional
- (void)likeButtonTappedForCell:(UITableViewCell *)cell;
- (void)commentButtonTappedForCell:(UITableViewCell *)cell;
- (void)shareButtonTappedForCell:(UITableViewCell *)cell;
@end
@interface YourCell : UITableViewCell
@property (nonatomic, weak) id <YourCellButtonsDelegate> buttonsDelegate;
@end

类似按钮的Action方法示例如下。

- (IBAction)likeButtonPressed:(id)sender {
[self.buttonsDelegate likeButtonTappedForCell:self];
}

在初始化并分配给YourCell后,在CellForRowAtIndex方法中的ViewController类中,按照以下分配委托

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"YourCellIdentifier";
YourCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//do your stuffs here.
cell.delegate = self
}

并按如下方式实现委托方法。

- (void)likeButtonTappedForCell:(UITableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//you can get the row from the indexPath here.
}

不要忘记在ViewController类中确认委托。

最新更新