ストーリーボードのプロトタイプセルにカスタムアクセサリビュー(UIButton)があります。そのボタンをクリックしても、タップされたアクセサリボタンのデリゲートメソッドが呼び出されません。私が標準の開示ボタンを使用する場合、それは問題なく呼ばれます。どこかにフックがないのではないかと思います。誰もがどこを知っていますか?
3 に答える
推測1:アクセサリが配置される場所に配置される
「カスタムアクセサリビュー」は実際にはありませんUIButton
。これは実際にはアクセサリではないため、デリゲートアクセサリのタップボタンは起動しません。
推測2:
実際のアクセサリビューはありますがUIButton
、ユーザーインタラクションを消費しているがアクションを実行していないため、「タップ」イベントが発生することはありません。UIView
その場合は、代わりに単純なものを追加してみてください。
それ以外はもっと情報が必要です。
アクセサリデリゲートメソッドのアップルドキュメントによると
代理人は通常、選択した行に関連する新しいビューを表示することにより、開示ボタン(アクセサリビュー)のタップに応答します。indexPathの行にアクセサリビューが設定されている場合、このメソッドは呼び出されません。
したがって、カスタムアクセサリビューには呼び出されません。これは標準的な動作です。
私は、カスタムテーブルセルのプロトコルを使用してこれを回避することになりました。
CustomAccessoryTableCellDelegate.h
@protocol CustomAccessoryTableCellDelegate <NSObject>
@required
- (void) accessoryButtonTappedForCell: (UITableViewCell *) cell;
@end
CustomAccessoryViewTableCell.h
@interface CustomAccessoryViewTableCell : UITableViewCell
/** The custom accessory delegate. */
@property (nonatomic, weak) id<CustomAccessoryTableCellDelegate> delegate;
@end
CustomAccessoryViewTableCell.m
- (IBAction) buttonAction:(id)sender{
if ([self.delegate respondsToSelector:@selector(accessoryButtonTappedForCell:)]) {
[self.delegate accessoryButtonTappedForCell:self];
}
}
テーブルビューコントローラーの内部
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomAccessoryViewTableCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MyCustomCell"];
cell.label.text = @"Some Name";
cell.delegate = self;
return cell;
}
#pragma mark - Custom Accessory View Delegate
- (void) accessoryButtonTappedForCell:(UITableViewCell *)cell{
[self tableView:self.writerTable
accessoryButtonTappedForRowWithIndexPath: [self.writerTable
indexPathForCell:cell]];
}