开发者

Go on Top in UITableView with a Searchbar

开发者 https://www.devze.com 2023-04-12 04:14 出处:网络
if I want that the TableView goes on Top, I have used a Button with this Code: [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

if I want that the TableView goes on Top, I have used a Button with this Code:

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

But its not really cool to use a Button to do this Action.

Is there a better way to do this? Should the User tapping 2x and then goes to the Top?

What is the b开发者_如何学编程est way ;)


Try looking at scrollToRowAtIndexPath:atScrollPosition:animated It's probably a better idea than scrolling with scrollRectToVisible.


Thanks for the Replies, i have solved the Problem within the Rows. If a User tapping longer on a Row, he will be pushed to Top.

Call that in your Function

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

Add this Function to your Class

//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];
    }
}

I hope that helps anyone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号