2014-11-03 54 views

回答

9

也有選項B,子類的UITableViewCell,並從UIResponder類的位置:

@interface CustomTableViewCell : UITableViewCell 

@property (nonatomic) CGPoint clickedLocation; 

@end 

@implementation CustomTableViewCell 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 
    self.clickedLocation = [touch locationInView:touch.view]; 
} 

@end 

然後從TableViewCell它自身獲得位置:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    //get the cell 
    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; 
    //get where the user clicked 
    if (cell.clickedLocation.Y<50) { 
     //Method A 
    } 
    else { 
     //Method B 
    } 
} 
1

假設你有一個自定義UICollectionViewCell,你可以添加一個UITapGestureRecognizer到單元格中,並在touchesBegan處理程序中獲取觸點。像這樣:

//add gesture recognizer to cell 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init]; 
[cell addGestureRecognizer:singleTap]; 

//handler 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 

    if (touchPoint.y <= 50) { 
     [self methodA]; 
    } 
    else { 
     [self methodB]; 
    } 
}