Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more extension methods for UITableView #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion Sources/UIKit/UITableView-Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,68 @@ public extension UITableView {
var sectionsIndexSet: IndexSet {
return IndexSet(integersIn: 0..<self.numberOfSections)
}
var visibleTopIndexPath: IndexPath? {
let visibleRows = indexPathsForVisibleRows ?? []
let firstPath: IndexPath
let secondPath: IndexPath

if visibleRows.isEmpty {
return nil
} else if visibleRows.count == 1 {
return visibleRows.first
} else {
firstPath = visibleRows[0]
secondPath = visibleRows[1]
}

let firstRowRect = rectForRow(at: firstPath)
return firstRowRect.origin.y > contentOffset.y ? firstPath : secondPath
}

}


extension UITableView {
public extension UITableView {
func reloadWithoutAnimation(){
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
self.reloadData()
CATransaction.commit()
}
}


public extension UITableView {
func scrollToLastRow() {
let indexPath = IndexPath(row:0,section: 0)
self.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
func scrollToFirstRow() {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: .top, animated: true)
}
func scrollToSelectedRow() {
let selectedRows = self.indexPathsForVisibleRows
if let selectedRow = selectedRows?[0] as NSIndexPath? {
self.scrollToRow(at: selectedRow as IndexPath, at: .middle, animated: true)
}
}
func scrollToHeader() {
self.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
func removeEliminateExtraSeparators() {
self.tableFooterView = UIView(frame: .zero)
self.tableHeaderView = UIView(frame:.zero)
}
func registerCell(_ cellTypes:[AnyClass]) {
for cellType in cellTypes {
let typeString = String(describing: cellType)
let xibPath = Bundle(for: cellType).path(forResource: typeString, ofType: ".nib")
if xibPath == nil {
register(cellType, forCellReuseIdentifier: typeString)
} else {
register(UINib(nibName: typeString, bundle: nil), forCellReuseIdentifier: typeString)
}
}
}
}