Как прокрутить до последней строки последнего раздела в UITableView? - PullRequest
0 голосов
/ 21 января 2020

Итак, у меня есть UITableView, который выбирает данные из массива объекта, скажем Чаты . Чтобы прокрутить до последней строки этого массива, я знаю, что мы используем:

var chats = [Chats]()
self.tableView.scrollToRow(at: [0, self.chats.count - 1], at: .bottom, animated: true)

Что делать, если у меня есть массив массива (это позволяет группировать сообщения чата в разделы по дате). Теперь у меня есть разделы, а затем строки под этим разделом.

var groupedChats = [[Chats]]()

Как перейти к последнему ряду последнего раздела?

Ответы [ 5 ]

2 голосов
/ 21 января 2020

Попробуйте это

    let lastSection = groupedChats.count-1
    let lastRow = groupedChats[lastSection].count-1
    if lastSection >= 0, lastRow >= 0 {
        self.tableView.scrollToRow(at: IndexPath(row: lastRow, section: lastSection), at: .bottom, animated: true)
    }

1 голос
/ 21 января 2020

Лучше создать расширение для повторного использования:

extension UITableView {

    func scrollToBottom(){

        DispatchQueue.main.async {
            let indexPath = IndexPath(
                row: self.numberOfRows(inSection:  self.numberOfSections-1) - 1,
                section: self.numberOfSections-1)
            self.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }
}
1 голос
/ 21 января 2020

Поиск последнего раздела и последней строки и прокрутка до последней строки indexpath.

Swift 5:

let lastSection = tableView.numberOfSections - 1
let lastRow = tableView!.numberOfRows(inSection: lastSection)
let lastRowIndexPath = IndexPath(row: lastRow, section: lastSection)
tableView.scrollToRow(at: lastRowIndexPath, at: .top, animated: true)
0 голосов
/ 21 января 2020

Вы можете попробовать это,

let lastSectionIndex = self.groupedChats.numberOfSections - 1 // last section
let lastRowIndex = self.groupedChats.numberOfRows(inSection: lastSectionIndex) - 1 // last row
self.tableView.scrollToRow(at: IndexPath(row: lastRowIndex, section: lastSectionIndex), at: .Bottom, animated: true)
0 голосов
/ 21 января 2020
 func scrollToBottom(_ animated: Bool = true) {
        let numberOfSections = self.tblView.numberOfSections
        if numberOfSections > 0 {
            let numberOfRows = self.tblView.numberOfRows(inSection: numberOfSections - 1)
            if numberOfRows > 0 {
                let indexPath = IndexPath(row: numberOfRows - 1, section: (numberOfSections - 1))
                self.tblView.scrollToRow(at: indexPath, at: .bottom, animated: animated)
            }
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...