2014-11-03 2 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 в ячейку и получить точку касания в обработчике touchhesBegan. Пример:

//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]; 
    } 
} 
Смежные вопросы