LoginSignup
11

More than 5 years have passed since last update.

iOS8でUITableViewCell.contentViewに仕込んだUIGestureRecognizerから、操作元のUITableViewCellにアクセスする方法

Last updated at Posted at 2014-09-20

UITableViewCellにUIGestureRecognizerを仕込んでる人は注意したほうがいいかも

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

のなかでこんなかんじに

UILongPressGestureRecognizer *lpg = [[UILongPressGestureRecognizer alloc]
                                              initWithTarget:self
                                              action:@selector(cellLongTapped:)];
[cell.contentView addGestureRecognizer:lpg];   

contentViewにUILongPressGestureRecognizerなんかを仕込んで
セルをロングタップした際に、cellの中身を見てごにょごにょしたい!というときに注意です。

UIGestureRecognizerから操作元のUITableViewCell見たいときに、iOS6,7,8でやり方が違う

- (void)cellLongTapped:(UILongPressGestureRecognizer *)sender
{
        UITableViewCell *cell = nil;
        if (7.0 <= IOS_VERSION && IOS_VERSION < 8.0){
            //ios7.XXの処理            
            cell = (UITableViewCell *)[[sender.view superview] superview];
        }
        else{
            //iOS6,8での処理
            cell = (UITableViewCell *)[sender.view superview];
        }
}

上記の例だと、これでロングタップされたcellをUITableViewCellで受け取れます
なんでiOS7だけ違うんだろう・・??

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11