2017-12-27 28 views
0

私は自分のセルでタッチを検出する方法をすでに見つけましたが、1つのセクションで作成されています。私は1を超えている(8 tbh)。次のコードでは、1つではなくいくつかのセルに触れています。だから問題は、私の問題の明確な解決策は何ですか?ここでは、コード:UITableViewCellのUIViewで1つ以上のセクションをタッチする方法を教えてください。

- (CatalogItemTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
CatalogItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 

CatalogItemModel *currentItemModel; 

if(isCatSelected) { 
    currentItemModel = [[self.itemsArray objectForKey:[[self.sectionArray objectAtIndex:selectedIndexPath] valueForKey:@"id"]] objectAtIndex:indexPath.row]; 
} else { 
    currentItemModel = [[self.itemsArray objectForKey:[[self.sectionArray objectAtIndex:indexPath.section] valueForKey:@"id"]] objectAtIndex:indexPath.row]; 
} 

[cell setItemModel:currentItemModel]; 

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                        action:@selector(didTapFav:)]; 
[cell.overFavorite addGestureRecognizer:gestureRecognizer]; //overFavorite is UIView to detect touch 

return cell; 
} 

アクションFUNC:CatalogItemTableViewCell内部

-(void)didTapFav:(UITapGestureRecognizer*)recognizer { 
NSString *token = [ProfileManager getToken]; 
if(token.length > 0){ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addToFavorite" object:self]; 
} else { 
    [self showErrorWithTitle:@"Auth" andText:@"To add item in Favorite!"]; 
} 

}

機能:

-(void)addToFavorite { 
    if(isFavorite) { 
     [ApiManager tryAddToFavItem:[NSString stringWithFormat:@"%ld", (long)self.currentModel.uid] :^{ 
      //Some API request 
     }]; 
    } 
} 
+0

委任パターンまたはクロージャを使用します。 [これらの回答](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#を参照してください。 38941510) – Paulw11

+0

ターゲットビューにタグを付けます。 –

+0

@ElTomatoは、インデックスパスを表すためにタグを使用しません。行を挿入、削除、または移動できる場合は失敗します。 – rmaddy

答えて

0

まず第一に、あなたのCollectionViewためgestureRecognizerを追加

UIGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; tapGesture.delegate = self; [self.collectionView addGestureRecognizer:tapGesture];

次にアクセスするIndexPathまたはCellを取得する - (void)tap:(UITapGestureRecognizer *)sender;

if (sender.state == UIGestureRecognizerStateEnded) { 

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[sender locationInView:self.collectionView]]; 
} 
関連する問題