2013-02-26 22 views
0

我有一個TableView,其中包含含有多個標籤的自定義單元格。我在這些標籤中的一個上添加了一個tapgesture,這樣彈出窗口就會出現。問題是,彈出窗口不會出現在應該出現的位置(居中且位於標籤頂部)。在TableViewCell上使用Tapgesture彈出框

這有什麼錯行:

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

tableview.m:

- (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)]; 
    } 
} 
+0

單元格變量可能成爲零。你最好做一個檢查程序。 – 2013-02-26 10:57:17

回答

1

你想彈出,一旦任何觀點:

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

的點觸手勢的方法細胞被挖掘?如果是,那麼我建議您在CustomCell類中使用-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法。

您將能夠使用該方法的參數event知道水龍頭的位置。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 
    for (UITouch *touch in allTouches) 
    { 
     CGPoint location = [touch locationInView:touch.view]; 
     (...) 
    } 
} 
+0

@Jack感謝您的修改。我的回答看起來比以前更好,更自然。 – 2014-02-05 02:55:59

+0

沒問題,您歡迎。 – Jack 2014-02-05 14:53:38