我正在寻找一种在单元格中使用按钮的方法。我已经在我的表格视图的原型单元格中创建了它,但是当我点击它时,我无法检测它所在的单元格。
你能帮帮我吗?答案 0 :(得分:1)
假设这个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
答案 1 :(得分:0)
“我在自定义单元格中处理按钮的方式:
“
的回答答案 2 :(得分:0)
- (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];
}
}