【发布时间】:2014-02-27 22:46:23
【问题描述】:
我正在寻找一种在单元格中使用按钮的方法。我在表格视图的原型单元格中创建了它,但是当我点击它时无法检测到它所在的单元格。
你能帮帮我吗?
【问题讨论】:
我正在寻找一种在单元格中使用按钮的方法。我在表格视图的原型单元格中创建了它,但是当我点击它时无法检测到它所在的单元格。
你能帮帮我吗?
【问题讨论】:
假设这个 IBAction 是您点击按钮时的事件处理程序:
- (IBAction) buttonTapped: (UIButton *) sender
{
//get parent cell
UITableViewCell *cell = [sender findSuperViewWithClass:[UITableViewCell class]];
//do whatever you want...
}
你需要这个类别的地方:
@implementation UIView (SuperView)
- (UIView *)findSuperViewWithClass:(Class)superViewClass
{
UIView *superView = self.superview;
UIView *foundSuperView = nil;
while (nil != superView && nil == foundSuperView) {
if ([superView isKindOfClass:superViewClass]) {
foundSuperView = superView;
break;
} else {
superView = superView.superview;
}
}
return foundSuperView;
}
@end
【讨论】:
“我在自定义单元格中处理按钮的方式:
"
【讨论】:
- (void)buttonOnCellTapped:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.table];
NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:buttonPosition];
if (indexPath)
{
NSLog(@"BUTTON TAPPED ON SECTION: %d, ROW: %d", indexPath.section, indexPath.row);
UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];
}
}
【讨论】: