私は自分のセルでタッチを検出する方法をすでに見つけましたが、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
}];
}
}
委任パターンまたはクロージャを使用します。 [これらの回答](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#を参照してください。 38941510) – Paulw11
ターゲットビューにタグを付けます。 –
@ElTomatoは、インデックスパスを表すためにタグを使用しません。行を挿入、削除、または移動できる場合は失敗します。 – rmaddy