简体   繁体   中英

UITableView scroll to bottom

I have this line of code:

tableView.contentOffset = CGPointMake(0.0f, 10000000.0f);

The content size is a lot less than the 10000000.0f , but the UITableView still does not scroll to the bottom. How can I do it?

Scrolling to tableViewCell ?

//for instance, you have 15 cells
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:14 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
                      atScrollPosition:UITableViewScrollPositionTop
                              animated:YES];

You can create an extension of UIScrollView , and use it for any instance of UIScrollView, UITableView or UICollectionView.

extension UIScrollView {

    func scrollToBottom(animated: Bool) {
        var y: CGFloat = 0.0
        let HEIGHT = self.frame.size.height
        if self.contentSize.height > HEIGHT {
            y = self.contentSize.height - HEIGHT
        }
        self.setContentOffset(CGPointMake(0, y), animated: animated)
    }
}

Whenever the content size if greater than scroll view's height, it will scroll accordingly to the correct position. This method works with AutoLayout also.

Swift 5, iOS 12 tested

👇 This code works perfectly for UITableView , even if there are sections with 0 elements (which will crash in every other solution I saw on the Internet)

extension UITableView {
    func scrollTableViewToBottom(animated: Bool) {
        guard let dataSource = dataSource else { return }

        var lastSectionWithAtLeasOneElements = (dataSource.numberOfSections?(in: self) ?? 1) - 1

        while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeasOneElements) < 1 {
            lastSectionWithAtLeasOneElements -= 1
        }

        let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeasOneElements) - 1

        guard lastSectionWithAtLeasOneElements > -1 && lastRow > -1 else { return }

        let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeasOneElements)
        scrollToRow(at: bottomIndex, at: .bottom, animated: animated)
    }
}

fixed version from Kevin in this topic, preventing infinite scroll for tableView with 0 items: Source: https://stackoverflow.com/a/59470799/9763761

func scrollToBottom(animated: Bool) {
    guard let dataSource = dataSource else { return }
    var lastSectionWithAtLeastOneElement = (dataSource.numberOfSections?(in: self) ?? 1) - 1
    while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) < 1 && lastSectionWithAtLeastOneElement > 0 {
        lastSectionWithAtLeastOneElement -= 1
    }
    let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) - 1
    guard lastSectionWithAtLeastOneElement > -1 && lastRow > -1 else { return }
    let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeastOneElement)
    scrollToRow(at: bottomIndex, at: .bottom, animated: animated)
}

👇 This code works for UIScrollView , but won't work for UITableView that have more cells then one screen can fit, because of Apple's weird internal implementation of UITableViewController :

extension UIScrollView {
    func scrollToBottom(animated: Bool) {
        guard contentSize.height > bounds.size.height else { return }

        let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
        setContentOffset(bottomOffset, animated: true)
    }
}

Unfortunately I don't yet have enough rep to comment, but vol's answer has 1 mistake in it which causes an infinite while loop when you call it on a tableview with 0 items, aside from that it works perfectly! Corrected code below:

func scrollToBottom(animated: Bool) {
    guard let dataSource = dataSource else { return }
    var lastSectionWithAtLeastOneElement = (dataSource.numberOfSections?(in: self) ?? 1) - 1
    while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) < 1 && lastSectionWithAtLeastOneElement > 0 {
        lastSectionWithAtLeastOneElement -= 1
    }
    let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeastOneElement) - 1
    guard lastSectionWithAtLeastOneElement > -1 && lastRow > -1 else { return }
    let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeastOneElement)
    scrollToRow(at: bottomIndex, at: .bottom, animated: animated)
}

try this code:

self.tableView.reloadData()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.1, execute: {
    let indexPath = IndexPath(row: self.dateSource.count-1, section: 0)
    self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: true)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM