mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-25 09:11:13 +01:00
47dc34db5d
This contains work related to issue #1910
38 lines
1.1 KiB
Swift
38 lines
1.1 KiB
Swift
//
|
|
// UITableView-Extensions.swift
|
|
// RSCoreiOS
|
|
//
|
|
// Created by Maurice Parker on 9/6/19.
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
extension UITableView {
|
|
|
|
/**
|
|
Selects a row and scrolls it to the middle if it is not visible
|
|
*/
|
|
public func selectRowAndScrollIfNotVisible(at indexPath: IndexPath, animations: Animations) {
|
|
selectRow(at: indexPath, animated: animations.contains(.select), scrollPosition: .none)
|
|
|
|
if let visibleIndexPaths = indexPathsForRows(in: safeAreaLayoutGuide.layoutFrame) {
|
|
if !(visibleIndexPaths.contains(indexPath) && cellCompletelyVisible(indexPath)) {
|
|
scrollToRow(at: indexPath, at: .middle, animated: animations.contains(.scroll))
|
|
}
|
|
}
|
|
}
|
|
|
|
func cellCompletelyVisible(_ indexPath: IndexPath) -> Bool {
|
|
let rect = rectForRow(at: indexPath)
|
|
return safeAreaLayoutGuide.layoutFrame.contains(rect)
|
|
}
|
|
|
|
public func middleVisibleRow() -> IndexPath? {
|
|
if let visibleIndexPaths = indexPathsForRows(in: safeAreaLayoutGuide.layoutFrame), visibleIndexPaths.count > 2 {
|
|
return visibleIndexPaths[visibleIndexPaths.count / 2]
|
|
}
|
|
return nil
|
|
}
|
|
}
|