I create tableView with sections, i use custom cell and define there checkbox(UIImageView) with image in this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cellIdentifier";
StandardCellWithImage *cell = (StandardCellWithImage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[StandardCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.checkbox.tag = indexPath.row;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectedImageAtIndexPath:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = self;
[cell.checkbox addGestureRecognizer:tapGesture];
cell.checkbox.userInteractionEnabled = YES;
return cell;
}
And in didSelectedImageAtIndexPath method i use:
- (void) didSelectedImageAtIndexPath:(id) sender {
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0];
}
But i've got here only row without any knowledge of on which section user tap this row. Is there any possibility to recognize it?
What about if you encode item/section within view.tag like this:
view.tag = indexPath.section * kMAX_SECTION_SIZE + indexPath.item;
then you could do:
- (void) didSelectedImageAtIndexPath:(id) sender {
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag % kMAX_SECTION_SIZE
inSection:int(gesture.view.tag / kMAX_SECTION_SIZE)];
}
Instead of adding gesture on checkbox(button in cell) in cellForRowAtIndexPath, implement button action in cell(StandardCellWithImage) itself and call delegate from there.
Note:
You can store a weak reference to the tableView in the cell, which you'd set in -tableView:cellForRowAtIndexPath: of your table's dataSource. This is better because relying on self.superview to always be exactly the tableView is fragile. Who knows how Apple might re-organize the view hierarchy of UITableView in the future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.