2013-12-16 36 views
0

我已經有我的插入方法以及編輯。但我想知道的是如何識別一個觸摸事件時,只點擊tablview插入?這與提醒應用程序類似,您可以在UITableView中的任何位置觸摸以插入新記錄。點擊時UITABLEVIEW呼叫插入方法

UITableView *tableView = mytableview; 
CGPoint tableLocation = [touch locationInView:tableView]; 

// this one recognises when I edit a record on a UITableview 
if([touch.view isKindOfClass:[UITableViewCell class]]) { 
    return NO; 
} 

if([touch.view.superview isKindOfClass:[UITableViewCell class]]) { 
    return NO; 
} 

if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) { 
    return NO; 
} 

// this one recognises if I tap on a UITableView 
if ([tableView hitTest:tableLocation withEvent:Nil]) { 


     //differentiate scrolling from tapping to invoke add method 

} 
else 
{ 

} 
+0

也許在UITableView的一個UITapGestureRecognizer? – dklt

+0

非常感謝我以前已經完成了這項工作,如果有任何問題,我只需要尋求替代解決方案。 –

+0

對,也許然後通過visibleCells循環+檢測使用rectForRowAtIndexPath插入到上面或下面的哪個單元格? – dklt

回答

0
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    //check if your gesture is only TAP so you could scroll the uitableview 

    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 

    //making sure when you tap, you only tap within the uitableview 

    UITableView *lemak_table = datetable; 
    CGPoint tapaw_Kopi = [gestureRecognizer locationInView:lemak_table]; 

    if ([lemak_table hitTest:tapaw_Kopi withEvent:Nil]) { 
     //Check if table is in editmode so you could tap buttons like "delete" 
     if (![lemak_table isEditing]) { 
      //INSERT METHOD HERE and NOWHERE ELSE FOR THAT MATTER. 
     } 

    } 

    } 
    return YES; 
} 


// I also added this method so you could edit exisiting records per row/cell thus differentiating TAP to Add from TAP to edit. 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch 
    { 

    if([touch.view isKindOfClass:[UITableViewCell class]]) { 
     return NO; 
    } 

    if([touch.view.superview isKindOfClass:[UITableViewCell class]]) { 
     return NO; 
    } 

    if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) { 
     return NO; 
    } 

    return YES; 
    }