iOS/Objective-C:在自定义Tableview单元格中检测到按下按钮? [英] IOS/Objective-C: Detect Button Press in Custom Tableview Cell?
问题描述
对于供稿View控制器,我有一个带有一些自定义单元格的表格视图,这些自定义单元格具有用于点赞,评论和分享的按钮,我希望根据按下哪个按钮来执行不同的操作.
我最初的想法是简单地将情节提要中自定义单元格中的按钮连接到自定义单元格中的操作方法.但是,我对自定义单元.m文件中的方法如何与视图控制器中的TableView交互感到困惑.
- (IBAction)likeButtonPressed:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
//do something
}
任何人都可以澄清是否可以在情节提要中连接单元格中的按钮按下吗,以及这如何与视图控制器,cellforrowatindexpath和didselectrowatindexpath上的委托方法一起使用吗?
或者,如果这不是正确的方法,请欣赏关于更好方法的任何建议.预先感谢您的任何建议.
您可以简单地使用 blocks
来代替使用delegate
或tags
.与delegate
模式相比,Blocks
更加易于使用和推荐.
在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();
}
For a feed View controller, I have a tableview with some custom cells that have buttons for like, comment and share I would like to perform different actions depending on which button is pushed.
My initial thought was to simply wire the buttons in the custom cell in storyboard to action methods in the custom cell. However, I have gotten confused on how methods in the custom cell .m file interact with the TableView in the View Controller.
- (IBAction)likeButtonPressed:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
//do something
}
Can anyone clarify whether a button push in a cell can be wired in storyboard and how this plays with the delegate methods on the viewcontroller, cellforrowatindexpath and didselectrowatindexpath?
Or if this is not the right way to do it, would appreciate any suggestions on better way. Thanks in advance for any suggestions.
Instead of using the delegate
or tags
, you can simply use blocks
to do that. Blocks
are much more easy and simple to use than the delegate
pattern and recommended.
In Swift
too, you shall see the extensive use of closures (blocks in Objective-C)
than any other pattern.
Example:
1. UITableViewDataSource
Methods
- (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. Custom UITableView Cell
@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();
}
这篇关于iOS/Objective-C:在自定义Tableview单元格中检测到按下按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!