I have a custom complex UITableViewCell where there are many views. I have an UIImageView within it which is visible for a specific condition. When it\'s visible ,
Here, We have customtableviewcell both .h and .m files with two images in cell. And HomeController, which have tableview to access this cell. This is detect the Tap on both the UIImage as described.
**CustomTableViewCell.h**
@protocol CustomTableViewCellDelegate
- (void)didTapFirstImageAtIndex:(NSInteger)index;
-(void)didTapSettingsImagAtIndex:(NSInteger)index;
@end
@interface CustomTableViewCell : UITableViewCell
{
UITapGestureRecognizer *tapGesture;
UITapGestureRecognizer *tapSettingsGesture;
}
@property (weak, nonatomic) IBOutlet UIImageView *firstImage;
@property (weak, nonatomic) IBOutlet UIImageView *lightSettings;
@property (nonatomic, assign) NSInteger cellIndex;
@property (nonatomic, strong) iddelegate;
**CustomTableViewCell.m**
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self addGestures];
}
- (void)addGestures {
tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapFirstImage:)];
tapGesture.numberOfTapsRequired = 1;
[_firstImage addGestureRecognizer:tapGesture];
tapSettingsGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapSettingsImage:)];
tapSettingsGesture.numberOfTapsRequired = 1;
[_lightSettings
addGestureRecognizer:tapSettingsGesture];
}
- (void)didTapFirstImage:(UITapGestureRecognizer *)gesture
{
if (_delegate) {
[_delegate didTapFirstImageAtIndex:_cellIndex];
}
}
-(void)didTapSettingsImage: (UITapGestureRecognizer
*)gesture {
if(_delegate) {
[_delegate didTapSettingsAtIndex:_cellIndex];
}
}
**HomeController.h**
#import
#import "CustomTableViewCell.h"
@interface HomeController : CustomNavigationBarViewController
@end
**HomeController.m**
---------------------
#import "HomeController.h"
#import "CustomTableViewCell.h"
@implementation HomeController
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return 2 (Number of rows) ;
// return number of rows
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"cell"
forIndexPath:indexPath];
cell.delegate = self;
cell.cellIndex = indexPath.row;
cell.firstImage.userInteractionEnabled = YES;
cell.lightSettings.userInteractionEnabled = YES;
return cell;
}
-(void)didTapFirstImageAtIndex:(NSInteger)index
{
NSLog(@"Index %ld ", (long)index);
//Do whatever you want here
}
-(void)didTapSettingsAtIndex:(NSInteger)index
{
NSLog(@"Settings index %ld", (long)index);
// Do whatever you want here with image interaction
}
@end