由UITableView TapGesture

时间:2015-06-27 08:38:41

标签: objective-c xcode uitableview uitapgesturerecognizer

我尝试打开一个AlertView,即使我持有tableview行0.5秒。

我用于以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[longTap setMinimumPressDuration:0.5];
longTap.delegate = (id)self;
[self.view addGestureRecognizer:longTap];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;

[[NSUserDefaults standardUserDefaults] setObject:cellText forKey:@"CellNameToEdit"];

}

- (void)handleTapGesture:(UILongPressGestureRecognizer *)sender{

if (sender.state == UIGestureRecognizerStateBegan) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Titel" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];

    alert.tag = 1;

    [alert show];
}
}

此代码可以使用,但问题是,我必须在打开警报视图之前先单击该行。我希望你明白我的意思。

2 个答案:

答案 0 :(得分:0)

为什么要将UILongPressGestureRecognizer添加到self.view,如果您希望在点击表...添加UILongPressGestureRecognizerUITableView [self.youTableViewName addGestureRecognizer:longTap]时调用<RelattiveLayout xmlns:my="http://schemas.android.com/apk/res-auto"> <com.clippingtest.ViewClip android:layout_below="@id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content"> </com.clippingtest.ViewClip> </RelativeLayout> )它会工作正常。

有关如何操作的更多信息。请查看以下链接..

https://stackoverflow.com/a/3924965/1865424

答案 1 :(得分:0)

我使用了以下 GestureRecognizer 代码,可能对您有帮助。

#pragma mark - View Controller Life Cycle
@implementation ViewController <UIGestureRecognizerDelegate>

- (void)viewDidLoad
{
    UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
    [gesture1 setDelegate:self];
    [gesture1 setMinimumPressDuration:1];
    Tableview.userInteractionEnabled = YES;
    [Tableview addGestureRecognizer:recognizer];
}

#pragma mark - DidSelcelect kindof method
-(void)gestureAction:(UITapGestureRecognizer *) sender
{

    CGPoint touchLocation = [sender locationOfTouch:0 inView:self.Tableview];

    //here is indexpath
    NSIndexPath *indexPath = [self.Tableview indexPathForRowAtPoint:touchLocation];

    NSLog(@"%ld", (long)indexPath.row);

    //Do here what you want to do with Cell
    [self.Tableview selectRowAtIndexPath:indexPath
                                animated:YES
                          scrollPosition:UITableViewScrollPositionNone];

}