Не могу прокрутить вниз UICollectionview

У меня проблемы с прокруткой вниз, я пробовал другие решения, но безуспешно.

Я использую IGLiskit

Вот ошибка:

попытаться прокрутить до неверного пути индекса

Вот код:

    func initMessages() {
    DataService.call.retrieveMessages(roomID: room.id ?? "") { (success, error, messages) in
        if !success {
            print("error", error!.localizedDescription)
        } else {
            guard let messages = messages else {return}
            self.messages = messages                
            DispatchQueue.main.async {
                self.adapter.performUpdates(animated: true, completion: nil)
                let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
                self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
            }
        }
    }
}

1 ответ

Решение

У вас есть 2 раздела в collectionview?, это да, игнорируйте мой ответ, я надеюсь, что проблема в том, что вы пытаетесь получить доступ к разделу 1, у вас просто есть раздел 0, попробуйте изменить этот код:

func initMessages() {
    DataService.call.retrieveMessages(roomID: room.id ?? "") { (success, error, messages) in
        if !success {
            print("error", error!.localizedDescription)
        } else {
            guard let messages = messages else {return}
            self.messages = messages                
            DispatchQueue.main.async {
                self.adapter.performUpdates(animated: true, completion: nil)
                // Here in section
                let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
                self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
            }
        }
    }
}

РЕДАКТИРОВАТЬ: хорошо, если раздел не работает, попробуйте это расширение:

extension UICollectionView {
    func scrollToLast() {
        guard numberOfSections > 0 else {
            return
        }

        let lastSection = numberOfSections - 1

        guard numberOfItems(inSection: lastSection) > 0 else {
            return
        }

        let lastItemIndexPath = IndexPath(item: numberOfItems(inSection: lastSection) - 1,
                                          section: lastSection)
        scrollToItem(at: lastItemIndexPath, at: .bottom, animated: true)
    }
}

использование:

collectionView.scrollToLast()
Другие вопросы по тегам