IOS/Objective-C:检测自定义 Tableview 单元格中的按钮按下?

IOS/Objective-C: Detect Button Press in Custom Tableview Cell?

对于提要视图控制器,我有一个带有一些自定义单元格的表格视图,这些单元格具有用于点赞、评论和分享的按钮我想根据按下的按钮执行不同的操作。

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

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

任何人都可以阐明单元格中的按钮按下是否可以连接到情节提要中,以及它如何与 viewcontroller、cellforrowatindexpath 和 didselectrowatindexpath 上的委托方法一起使用?

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

您可以通过两种方式进行。最好记住第一个 Objective-C 设计模式。

  • 使用委托模式

    您可以通过按钮 ID(可以是标记或枚举)和单元格对象将调用发送回视图控制器并在那里处理内容。

  • 根据indexPath

    使用标签

    例如,如果您只有行而没有列,那么您可以根据行号和按钮类型分配按钮标签,如果您有行和部分,则可以根据行号和部分分配标签,然后将选择器分配给 CellForRowAtIndexPath 中的按钮然后您可以从解码 sender

  • 的标签中获取行号

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

在您的自定义单元格中 Class。

@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

“赞”按钮的操作方法示例如下。

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

并且在你的ViewController class in CellForRowAtIndex方法中初始化并赋值给YourCell后,如下赋值delegate

- (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 class 中确认委托。

您可以简单地使用 blocks 而不是使用 delegatetagsBlocksdelegate 模式更容易使用,推荐使用。

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();
}