2 ooomiao oOOMIAO 于 2013.01.21 10:21 提问

得知UIImageView中那个cell被点击

请问,我创建了tableView,用到自定义单元,用图片定义复选框(UIImageView),如下:

- (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;
}

在didSelectedImageAtIndexPath方法中:

- (void) didSelectedImageAtIndexPath:(id) sender {
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0];
}

目前我只能知道用户点击了哪行,我想知道用户点击的具体位置,不知道应该怎么实现?

1个回答

starwar2012
starwar2012   2013.01.21 11:14

编写view.tag中条目如下:

view.tag = indexPath.section * kMAX_SECTION_SIZE + indexPath.item;

然后

- (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)];
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!