0

我有一个包含多个标签的自定义单元格的 TableView。我在其中一个标签上添加了一个 Tapgesture,因此会出现一个弹出窗口。问题是,弹出窗口没有出现在它应该出现的位置(居中并位于标签顶部)。

这条线有什么问题:

[self.popupMenu showInView:self.view atPoint:CGPointMake(label.center.x, label.frame.origin.y)];

表视图.m:

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

    // add a tap gesture recognizer
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
    [cell.customAmountLabel addGestureRecognizer:tapGesture];

    NSString *ItemName = [NSString stringWithCString:CurrentOrder[indexPath.row].c_str() encoding:NSUTF8StringEncoding];
    NSString *ItemAmount = [NSString stringWithCString:CurrentOrder[indexPath.row].c_str() encoding:NSUTF8StringEncoding];

    cell.customNameLabel.text = ItemName;
    cell.customAmountLabel.text = ItemAmount;

    return cell;
}

点击手势的方法:

- (void)labelTap:(UITapGestureRecognizer *)gestureRecognizer
{
    // get location of the swipe
    CGPoint location = [gestureRecognizer locationInView:self.tableCurrentOrder];

    // get the corresponding index path within the table view
    NSIndexPath *indexPath = [self.tableCurrentOrder indexPathForRowAtPoint:location];

    // check if index path is valid
    if(indexPath)
    {
        // get the cell out of the table view
        CustomCell *cell = (CustomCell *) [self.tableCurrentOrder cellForRowAtIndexPath:indexPath];

        // update the cell or model
        std::cout << CurrentOrder[indexPath.row] << std::endl;

        UILabel *label = (UILabel *)cell.customAmountLabel;

        [self.popupMenu showInView:self.view atPoint:CGPointMake(label.center.x, label.frame.origin.y)];
    }
}
4

1 回答 1

1

您想在点击单元格后弹出任何视图吗?如果是,那么我建议您-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event在 CustomCell 类中使用该方法。

您将能够使用该event方法的参数知道水龙头在哪里。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches]; 
    for (UITouch *touch in allTouches) 
    { 
        CGPoint location = [touch locationInView:touch.view];
        (...)
    }
}
于 2013-02-26T10:52:28.353 回答