From a5583fc655d6ec887fe97edd371e9d9d8160c974 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Wed, 22 Apr 2020 18:18:20 -0500 Subject: [PATCH] Validate that the selected row hasn't been removed when trying to select or scroll to it. Issue #1976 --- iOS/UIKit Extensions/UITableView-Extensions.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/iOS/UIKit Extensions/UITableView-Extensions.swift b/iOS/UIKit Extensions/UITableView-Extensions.swift index 368680ee9..fd0c4c0ee 100644 --- a/iOS/UIKit Extensions/UITableView-Extensions.swift +++ b/iOS/UIKit Extensions/UITableView-Extensions.swift @@ -14,6 +14,13 @@ extension UITableView { Selects a row and scrolls it to the middle if it is not visible */ public func selectRowAndScrollIfNotVisible(at indexPath: IndexPath, animations: Animations) { + guard let dataSource = dataSource, + let numberOfSections = dataSource.numberOfSections, + indexPath.section < numberOfSections(self), + indexPath.row < dataSource.tableView(self, numberOfRowsInSection: indexPath.section) else { + return + } + selectRow(at: indexPath, animated: animations.contains(.select), scrollPosition: .none) if let visibleIndexPaths = indexPathsForRows(in: safeAreaLayoutGuide.layoutFrame) {