iPhone SDK - How to scroll a UITableView programmatically with animation?

前端 未结 4 1433
傲寒
傲寒 2021-02-19 21:15

How would I scroll a UITableView to a specific position with an animation?

Currently I\'m using this code to jump to a position:

 //tableController ->         


        
相关标签:
4条回答
  • 2021-02-19 21:52

    I have two suggestions for you to investigate:

    1. Are you running your app on the simulator or the iPhone itself? I have noticed some animations behave differently in the simulator.

    2. Is it possible you are calling scrollToRowAtIndexPath:atScrollPosition:animated twice in quick succession? This can 'confuse' the animation. You should call this function only once while processing a given event.

    0 讨论(0)
  • 2021-02-19 21:54

    It works fine for me:

    -(void) viewDidAppear:(BOOL)animated{
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:0];
        [self.tableView scrollToRowAtIndexPath:indexPath
                        atScrollPosition:UITableViewScrollPositionTop
                                animated:YES];
    
    }
    

    To be sure that this is called only once, you can make a verification like this:

    if(!isInitialized){
       isInitialized = YES;
       ....
    }
    
    0 讨论(0)
  • Try commenting out the reloadData call. If I'm understanding your question correctly, and you're in viewDidLoad, you should not need to call it anyway.

    Also, if you've just gotten done pushing the view controller, you won't get an animated scroll. You'll have to insert a delay (I've found a quarter second works well) between the time that viewDidLoad was called and when your animation starts.

    0 讨论(0)
  • 2021-02-19 22:01

    I know it's maybe too late, but maybe it is help to others, now you can scroll a table just with [tableView scrollsToTop].

    0 讨论(0)
提交回复
热议问题