나는이 질문에 나는 내가 TableViewController 셀의 버튼을 탭하고 알 수있는 방법 그들에 버튼과 사용자 정의 세포를 가지고있는 상황에서,의 생각을 물었을 때. 최근 책 "아이폰 OS 조리법"을 읽어, 나는 솔루션을 가지고 :
-(IBAction)cellButtonTapped:(id)sender
{
NSLog(@"%s", __FUNCTION__);
UIButton *button = sender;
//Convert the tapped point to the tableView coordinate system
CGPoint correctedPoint = [button convertPoint:button.bounds.origin toView:self.tableView];
//Get the cell at that point
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
NSLog(@"Button tapped in row %d", indexPath.row);
}
또 다른 해결책, 더 깨지기 쉬운 조금 (하지만 간단)이 될 것입니다 :
- (IBAction)cellButtonTapped:(id)sender
{
// Go get the enclosing cell manually
UITableViewCell *parentCell = [[sender superview] superview];
NSIndexPath *pathForButton = [self.tableView indexPathForCell:parentCell];
}
그리고 대부분의 재사용을 당신의있는 UITableViewController 클래스에 다음 jQuery과
- (NSIndexPath *)prp_indexPathForRowContainingView:(UIView *)view
{
CGPoint correctedPoint = [view convertPoint:view.bounds.origin toView:self];
return [self indexPathForRowAtPoint:correctedPoint];
}
의 범주에이 방법을 추가하는 것, 바로 이것을 사용 :
- (IBAction)cellButtonTapped:(id)sender
{
NSIndexPath *pathForButton = [self.tableView indexPathForRowContainingView:sender];
}
스폿이 켜져 있습니다. 일상적으로 침해당하는 가장 큰 문제는 '우려의 분리'입니다. –
감사합니다. David! 당신의 대답은 또한 iOS에서 위임 패턴과 느슨한 결합, 재사용 가능한 코드 작성에 대해 더 많이 이해하게했습니다. – Giovanni