点击时UITABLEVIEW调用插入方法

时间:2013-12-16 06:53:04

标签: cocoa-touch uitableview

我已经有了插入方法和编辑功能。但我想知道的是,当仅点击要插入的tablview时,如何识别触摸事件?这类似于Reminders应用程序,您可以在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
{

}

1 个答案:

答案 0 :(得分: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; 
  }