Проблема с ошибкой в ​​UITableView.scrollToBottom - PullRequest
0 голосов
/ 01 апреля 2020

Я начинаю разработчика в Swift, и у меня есть проблема с ошибкой в ​​TableView. Я не знаю, как повторить эту ошибку. Я перепробовал много решений и ничего.

Моя ошибка при сбое:

Fatal Exception: NSRangeException
Attempted to scroll the table view to an out-of-bounds section (2) when there are only 2 sections. Table view: <UITableView: 0x105038000; frame = (0 0; 375 524.5); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x28322c810>; layer = <CALayer: 0x283cffe60>; contentOffset: {0, 723.5}; contentSize: {375, 1248}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <Fotka.MessagesViewController: 0x1047ae870>>

Сначала я защитился от превышения значения:

 if (self.numberOfSections > lastIndexPath.section && self.numberOfRows(inSection: lastIndexPath.section) > lastIndexPath.row ) {
                DispatchQueue.main.async {
                    self.scrollToRow(at: lastIndexPath, at: .top, animated: animated)
                }
            }

И Fail, все еще ошибки. Сейчас попробую с do-catch (из любопытства, если получится).

private var lastIndexPath: IndexPath? {
        guard numberOfSections > 0 else { return nil }

        do {
            let lastSection = numberOfSections - 1
            let numberOfRowsInLastSection = numberOfRows(inSection: lastSection)

            guard numberOfRowsInLastSection > 0 else { return nil }
            let lastRow = numberOfRowsInLastSection - 1

            return IndexPath(row: lastRow, section: lastSection)
        } catch {
            return nil
        }
    }

    func scrollToBottom(animated: Bool) {
        do {
            guard let lastIndexPath = self.lastIndexPath else { return }

            if (self.numberOfSections > lastIndexPath.section && self.numberOfRows(inSection: lastIndexPath.section) > lastIndexPath.row ) {
                DispatchQueue.main.async {
                    try? self.scrollToRow(at: lastIndexPath, at: .top, animated: animated)
                }
            }
        } catch {
            print("Catch scroll error")
        }
    }

И выполнить:

  do {
            try tableView.scrollToBottom(animated: true)
        } catch {
            print("Error with scroll")
        }

Результат: ошибки все еще остаются.

Почему даже do-catch не останавливает эту ошибку?

Добавьте код, определяющий и использующий раздел:

MessagesViewController: https://pastebin.com/1Kj537W7

MessagesViewModel: https://pastebin.com/gmPeZ5sJ

Я не могу добавить все приложения кода, потому что это приложение компании.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...