2011-10-10 37 views
0

如果我想的是,TableView中那张上面,我使用了一个按钮使用此代码:顶上去的UITableView的一个搜索栏

[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; 

但它不是真正冷静地用一个按钮做这个动作。

有没有更好的方法来做到这一点?用户应该点击2x然后点击顶部?

什么是最好的办法;)

回答

0

试着看一下scrollToRowAtIndexPath:atScrollPosition:动画 这可能比scrollRectToVisible滚动一个更好的主意。

+0

好的,但问题是,什么是为用户激活此操作的最佳方式。 Funktion - (void)touchesBegan:(NSSet *)触及事件:(UIEvent *)event {}不起作用。如果用户(例如)在TableView中敲击两次,这个动作将如何被激活? – CaspervanDoorn

+0

没人?用Button做这件事真的很恶心。 – CaspervanDoorn

+0

如何将UITapGestureRecognizer添加到表视图?然后,您可以将“numberOfTapsRequired”的值更改为1,默认值为2。 – Stavash

0

感谢您的回复,我已经解决了行内的问题。如果用户在一排上的时间越长,他将被推到顶部。

呼叫,在你的函数

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

//Action -> long pressing on a Cell 
     UILongPressGestureRecognizer *longPressGesture = 
     [[[UILongPressGestureRecognizer alloc] 
      initWithTarget:self action:@selector(longPress:)] autorelease]; 
     [cell addGestureRecognizer:longPressGesture]; 
} 

这个功能添加到您的类

//Method 
- (void)longPress:(UILongPressGestureRecognizer *)gesture 
{ 
    // only when gesture was recognized, not when ended 
    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     // get affected cell 
     UITableViewCell *cell = (UITableViewCell *)[gesture view]; 

     // get indexPath of cell 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

     // do something with this action 
     NSLog(@"Long-pressed cell at row %@", indexPath); 

     //go to the first Row 
     [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; 
    } 
} 

我希望帮助任何人。