UITableView - scroll down to viewDidAppear

If this is not obvious by name, then I want to be simple, as it tableViewwill begin to scroll to the lowest level when users first see it ( before it sees it, and without animation ).

So, I know that this has been answered several times, but none of these solutions seem to be working right now. To give some context, I use Swift, autorun, and the latest version of iOS to date.

Limitations

There are several things that I need to support:

  • download it before the user sees it (without animation, obviously).
  • dynamic cell heights, i.e. their height is determined by UILabel (for example, a messaging application - using autorun in a storyboard).
  • My UITableViewis inside UIScrollView( UIScrollViewscrolls horizontally and UITableViewscrolls vertically).
  • UITableViewlocated in childViewController, i.e. I add it to the main one UIViewController(which for some reason does viewWillAppearnot get called - viewDidAppearis still being called).

I will do a summary of what I tried:

As for the implementations:

  • tableView.contentOffset = CGPoint(x: 0, y: CGFloat.max

  • tableView.scrollToRowAtIndexPath(indexPathForLastItem, atScrollPosition: .Bottom, animated: false)


    var contentOffset = tableView.contentOffset
    contentOffset.y = tableView.contentSize.height - tableView.bounds.size.height
    tableView.setContentOffset(contentOffset, animated: false)


    dispatch_async(dispatch_get_main_queue(), {
        // tried option 1., 2. and 3. here
    })

As for the places that I tried to call these implementations:

  • viewDidLoad
  • viewWillAppear
  • viewDidLayoutSubviews (only the first time he called, I use the property to track this)
  • viewDidAppear (although this will not give me what I want)
  • On my model didSet

Before what I always called tableView.reloadData

What I do NOT want to do:

tableView.transform = CGAffineTransformMakeScale(1, -1)

+

cell.transform = CGAffineTransformMakeScale(1, -1)

( , , , , , . , , , )

, , ...

, :

  • tableView contentSize ( UIScrollView) . , contentSize, contentOffset, . tableView .
  • , (, , - ), 20 .
  • viewWillAppear , viewDidAppear .

( , ) . , .

, scrollToRowAtIndexPath... , .. , , , 2000 . , .

:

  • @bsmith11 :

    private var called = false
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        if !called {
            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.setContentOffset(CGPoint(x: 0, y: self.tableView.bounds.height), animated: false)
            })
            called = true
        }
    }
    

    .

+4
6

viewDidLayoutSubviews() .contentOffset tableView.bounds.height. , tableView .

dispatch_async(dispatch_get_main_queue()) { self.tableView.setContentOffset(tableView.bounds.height, animated: false) }

viewDidLayoutSubviews() , , , , .

+2

, .

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

      myArray = [["Data": "1234567890", "Height": 44],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "12345678901234567890", "Height": 88],["Data": "End of Table", "Height": 132]]

    let no = myArray.count-1

    let lastIndex = NSIndexPath(forRow: no, inSection: 0)

    table1.scrollToRowAtIndexPath(lastIndex, atScrollPosition: .Bottom, animated: true)

}

+1

.

 - (void)tableViewScrollToBottomAnimated:(BOOL)animated {
        NSInteger numberOfRows = [_tableView numberOfRowsInSection:0];
        if (numberOfRows) {
            [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:numberOfRows-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:animated];
        }
    }

[self tableViewScrollToBottomAnimated:NO] viewDidAppear . , , tableView:heightForRowAtIndexPath: .

0

, , ( ), , , , scrollToBottom viewDidLayoutSubviews

scrollToBottom true,

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // Scroll table view to the last row
    [self scrollToBottom];
}

-(void)scrollToBottom {
    if (shouldScrollToLastRow)
    {
        CGPoint bottomOffset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.bounds.size.height);
        [self.tableView setContentOffset:bottomOffset animated:NO];
    } }

, , , , , , scrollViewDidScroll

-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
    float scrollViewHeight = scrollView.frame.size.height;
    float scrollContentSizeHeight = scrollView.contentSize.height;
    float scrollOffset = scrollView.contentOffset.y;

    // if you're not at bottom then scroll to bottom
    if (!(scrollOffset + scrollViewHeight == scrollContentSizeHeight))
    {
        [self scrollToBottom];
    } else {
    // bottom reached now stop scrolling
        shouldScrollToLastRow = false;
    }
}
0
private var called = false
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if !called {
        dispatch_async(dispatch_get_main_queue(), {
            self.tableView.setContentOffset(CGPoint(x: 0, y: self.tableView.bounds.height), animated: false)
        })
        called = true
    }
}

UIScrollView. . dispatch_async , .

Swift 3 code

private var called = false
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if !called {
        DispatchQueue.main.async(execute: {
            self.tableView.setContentOffset(CGPoint(x: 0, y: self.tableView.bounds.height), animated: false)})
        called = true
    }
}
0

, [table setContentOffset:CGPointMake(0, CGFLOAT_MAX)] table.estimatedRowHeight = 50(change your value); ViewDidLoad - , .

0

All Articles