iphone - didSelectRowAtIndexPath:仅在长按自定义单元格后才被调用

Ana*_*tam 34 objective-c uitableview ios

我正在创建一个基于表视图的应用程序.我为表创建了一个自定义表格单元格,其中包含2个标签,1个图像和1个按钮.表视图数据源方法正常工作.我正在为自定义单元格和视图控制器类使用xib,我将委托和数据源连接到文件的所有者.但问题是当我选择表格行时,didSelectRowAtIndexPath没有起火.如上所述,解雇它的唯一方法是按住电池约3-4秒.有谁知道为什么会这样?

谢谢你的任何指示......

这是我的表视图方法..

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [finalAddonsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NewCustomCell *cell = (NewCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"NewCustomCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }

    Addons *addons1=[[Addons alloc]init];
    addons1= [finalAddonsArray objectAtIndex:indexPath.row];

    if (addons1.data == nil) {
        cell.ivCategory.image = [UIImage imageNamed:@"blogo.jpg"];
    }
    else
    {
        cell.ivCategory.image=[UIImage imageWithData:addons1.data];
    }

    cell.lblTitle.text = addons1.name;
    if (addons1.price == nil) {
        cell.lblPrice.text = nil;
    }
    else{
        cell.lblPrice.text = [NSString stringWithFormat:@"%@ rs",addons1.price];
    }
    [cell.button addTarget:self
                    action:@selector(editButtonPressed:)
          forControlEvents:UIControlEventTouchUpInside];

    cell.button.tag=indexPath.row;
    index = indexPath;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"sjcjksbcjksbcfkebscf1234567890");
}
Run Code Online (Sandbox Code Playgroud)

我得到的另一件事是,如果我使用默认的UITableViewCell而不是自定义单元格,那么我的问题也是一样,委托方法不会起火.

自定义单元属性:

在此输入图像描述 在此输入图像描述

Man*_*wal 96

同样的问题发生在我身上,因为我在它上面添加了一个轻敲手势识别器.如果您使用过任何手势识别器,请尝试将其删除并检查是否导致问题.

编辑:由阿里评论的解决方案:如果您使用了点按手势,则可以使用 [tap setCancelsTouchesInView:NO];

  • 在你的点击手势中添加这个`[tap setCancelsTouchesInView:NO];` (14认同)