diff --git a/Source/MYBaseClasses.swift b/Source/MYBaseClasses.swift index c2388c3..3525045 100644 --- a/Source/MYBaseClasses.swift +++ b/Source/MYBaseClasses.swift @@ -8,7 +8,7 @@ import UIKit -typealias MYSelectionHandler = (MYBaseViewProtocol) -> () +public typealias MYSelectionHandler = (MYBaseViewProtocol) -> () public protocol MYBaseViewProtocol { func highlight(Bool) diff --git a/Source/MYHeaderFooterView.swift b/Source/MYHeaderFooterView.swift index c4249ee..58eadc0 100644 --- a/Source/MYHeaderFooterView.swift +++ b/Source/MYHeaderFooterView.swift @@ -54,17 +54,17 @@ public class MYHeaderFooterView : UITableViewHeaderFooterView, MYBaseViewProtoco delegate?.didSelect(view) } - func didChangeFloatingState(isFloating: Bool) { + public func didChangeFloatingState(isFloating: Bool) { } } // MARK - Hightlight public extension MYHeaderFooterView { - func highlight(animated: Bool) { + public func highlight(animated: Bool) { } - func unhighlight(animated: Bool) { + public func unhighlight(animated: Bool) { } } diff --git a/Source/MYTableViewCell.swift b/Source/MYTableViewCell.swift index 35941a3..4166424 100644 --- a/Source/MYTableViewCell.swift +++ b/Source/MYTableViewCell.swift @@ -84,11 +84,11 @@ public extension MYTableViewCell { override func setSelected(selected: Bool, animated: Bool) { } - func highlight(animated: Bool) { + public func highlight(animated: Bool) { super.setHighlighted(true, animated: animated) } - func unhighlight(animated: Bool) { + public func unhighlight(animated: Bool) { super.setHighlighted(false, animated: animated) } } diff --git a/Source/MYTableViewManager.swift b/Source/MYTableViewManager.swift index 0583c24..1b20ba0 100644 --- a/Source/MYTableViewManager.swift +++ b/Source/MYTableViewManager.swift @@ -121,131 +121,121 @@ public extension MYTableViewManager { // MARK - Append public extension MYTableViewManager { - public func appendData(data: MYTableViewCellData, inSection section: Int, reloadType: MYReloadType = .InsertRows(.None)) { + func appendData(data: MYTableViewCellData, inSection section: Int, reloadType: MYReloadType = .InsertRows(.None)) { let cellData = [data] self.appendData(cellData, inSection: section, reloadType: reloadType) } - public func appendData(data: [MYTableViewCellData], inSection section: Int, reloadType: MYReloadType = .InsertRows(.None)) { - //dispatch_async(dispatch_get_main_queue()) { - if self.dataSource.indexForKey(section) != nil { - self.setBaseViewDataDelegate(data) - self.dataSource[section]! += data + func appendData(data: [MYTableViewCellData], inSection section: Int, reloadType: MYReloadType = .InsertRows(.None)) { + if self.dataSource.indexForKey(section) != nil { + self.setBaseViewDataDelegate(data) + self.dataSource[section]! += data - switch reloadType { - case .InsertRows(let animation): - let startRowIndex = self.dataSource[section]!.count - data.count - let endRowIndex = startRowIndex + data.count - let indexPaths = (startRowIndex.. NSIndexPath in - return NSIndexPath(forRow: index, inSection: section) - } - self.tableView?.insertRowsAtIndexPaths(indexPaths, withRowAnimation: animation) + switch reloadType { + case .InsertRows(let animation): + let startRowIndex = self.dataSource[section]!.count - data.count + let endRowIndex = startRowIndex + data.count + let indexPaths = (startRowIndex.. NSIndexPath in + return NSIndexPath(forRow: index, inSection: section) + } + self.tableView?.insertRowsAtIndexPaths(indexPaths, withRowAnimation: animation) - case .ReloadSection(let animation): - let indexSet = NSIndexSet(index: section) - self.tableView?.reloadSections(indexSet, withRowAnimation: animation) + case .ReloadSection(let animation): + let indexSet = NSIndexSet(index: section) + self.tableView?.reloadSections(indexSet, withRowAnimation: animation) - case .None: - break + case .None: + break - default: - self.tableView?.reloadData() - } - return + default: + self.tableView?.reloadData() } - self.resetWithData(data, inSection: section, reloadType: reloadType) - //} + return + } + self.resetWithData(data, inSection: section, reloadType: reloadType) } } // MARK - Reset -extension MYTableViewManager { - /* - public func resetWithData(data: MYTableViewCellData, inSection section: Int, reloadType: MYReloadType = .ReloadSection(.None)) { +public extension MYTableViewManager { + func resetWithData(data: MYTableViewCellData, inSection section: Int, reloadType: MYReloadType = .ReloadSection(.None)) { resetWithData([data], inSection: section, reloadType: reloadType) } - */ - public func resetWithData(data: [MYTableViewCellData], inSection section: Int, reloadType: MYReloadType = .ReloadSection(.None)) { - //dispatch_async(dispatch_get_main_queue()) { - self.setBaseViewDataDelegate(data) - - let length = section + 1 - self.numberOfSections - let insertSections: NSIndexSet? = length > 0 ? NSIndexSet(indexesInRange: NSMakeRange(self.numberOfSections, length)) : nil - self.numberOfSections = max(self.numberOfSections, section + 1) - self.dataSource[section] = data + func resetWithData(data: [MYTableViewCellData], inSection section: Int, reloadType: MYReloadType = .ReloadSection(.None)) { + self.setBaseViewDataDelegate(data) + + let length = section + 1 - self.numberOfSections + let insertSections: NSIndexSet? = length > 0 ? NSIndexSet(indexesInRange: NSMakeRange(self.numberOfSections, length)) : nil + self.numberOfSections = max(self.numberOfSections, section + 1) + self.dataSource[section] = data - switch reloadType { - case .ReloadSection(let animation): - if insertSections != nil { - self.tableView?.insertSections(insertSections!, withRowAnimation: animation) - } else { - let indexSet = NSIndexSet(index: section) - self.tableView?.reloadSections(indexSet, withRowAnimation: animation) - } - - case .None: - break - - default: - self.tableView?.reloadData() + switch reloadType { + case .ReloadSection(let animation): + if insertSections != nil { + self.tableView?.insertSections(insertSections!, withRowAnimation: animation) + } else { + let indexSet = NSIndexSet(index: section) + self.tableView?.reloadSections(indexSet, withRowAnimation: animation) } - //} + + case .None: + break + + default: + self.tableView?.reloadData() + } } } // MARK - Insert public extension MYTableViewManager { - public func insertData(data: MYTableViewCellData, inSection section: Int, atRow row: Int, reloadType: MYReloadType = .InsertRows(.None)) { + func insertData(data: MYTableViewCellData, inSection section: Int, atRow row: Int, reloadType: MYReloadType = .InsertRows(.None)) { self.insertData([data], inSection: section, atRow: row) } - public func insertData(data: [MYTableViewCellData], inSection section: Int, atRow row: Int, reloadType: MYReloadType = .InsertRows(.None)) { - //dispatch_async(dispatch_get_main_queue()) { - self.setBaseViewDataDelegate(data) + func insertData(data: [MYTableViewCellData], inSection section: Int, atRow row: Int, reloadType: MYReloadType = .InsertRows(.None)) { + self.setBaseViewDataDelegate(data) - if self.dataSource[section] == nil { - var rt: MYReloadType = .None - switch reloadType { - case .None: - rt = .None - default: - rt = .ReloadSection(.None) - } - return self.resetWithData(data, inSection: section, reloadType: rt) - } - if row < 0 || row > self.dataSource[section]!.count { - return + if self.dataSource[section] == nil { + var rt: MYReloadType = .None + switch reloadType { + case .None: + rt = .None + default: + rt = .ReloadSection(.None) } - self.dataSource[section]?.insert(data, atIndex: row) + return self.resetWithData(data, inSection: section, reloadType: rt) + } + if row < 0 || row > self.dataSource[section]!.count { + return + } + self.dataSource[section]?.insert(data, atIndex: row) - switch reloadType { - case .InsertRows(let animation): - let startRowIndex = row - let endRowIndex = startRowIndex + data.count - let indexPaths = (startRowIndex.. NSIndexPath in - return NSIndexPath(forRow: index, inSection: section) - } - self.tableView?.insertRowsAtIndexPaths(indexPaths, withRowAnimation: animation) + switch reloadType { + case .InsertRows(let animation): + let startRowIndex = row + let endRowIndex = startRowIndex + data.count + let indexPaths = (startRowIndex.. NSIndexPath in + return NSIndexPath(forRow: index, inSection: section) + } + self.tableView?.insertRowsAtIndexPaths(indexPaths, withRowAnimation: animation) - case .ReloadSection(let animation): - let indexSet = NSIndexSet(index: section) - self.tableView?.reloadSections(indexSet, withRowAnimation: animation) + case .ReloadSection(let animation): + let indexSet = NSIndexSet(index: section) + self.tableView?.reloadSections(indexSet, withRowAnimation: animation) - case .None: - break + case .None: + break - default: - self.tableView?.reloadData() - } - //} + default: + self.tableView?.reloadData() + } } - public func insertDataBeforeLastRow(data: [MYTableViewCellData], inSection section: Int, reloadType: MYReloadType = .InsertRows(.None)) { - //dispatch_async(dispatch_get_main_queue()) { - let lastRow = max((self.dataSource[section]?.count ?? 0) - 1, 0) - self.insertData(data, inSection: section, atRow: lastRow, reloadType: reloadType) - //} + func insertDataBeforeLastRow(data: [MYTableViewCellData], inSection section: Int, reloadType: MYReloadType = .InsertRows(.None)) { + let lastRow = max((self.dataSource[section]?.count ?? 0) - 1, 0) + self.insertData(data, inSection: section, atRow: lastRow, reloadType: reloadType) } } @@ -261,60 +251,56 @@ public extension MYTableViewManager { } func removeDataInSection(section: Int, inRange range: Range, reloadType: MYReloadType = .DeleteRows(.None)) { - //dispatch_async(dispatch_get_main_queue()) { - if self.dataSource[section] != nil { - let start = max(0, range.startIndex) - let end = min(self.dataSource[section]!.count, range.endIndex) - let safeRange = Range(start: start, end: end) - self.dataSource[section]!.removeRange(safeRange) + if self.dataSource[section] != nil { + let start = max(0, range.startIndex) + let end = min(self.dataSource[section]!.count, range.endIndex) + let safeRange = Range(start: start, end: end) + self.dataSource[section]!.removeRange(safeRange) + + switch reloadType { + case .DeleteRows(let animation): + let indexPaths = safeRange.map { NSIndexPath(forRow: $0, inSection: section) } + self.tableView?.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: animation) + + case .ReloadSection(let animation): + let indexSet = NSIndexSet(index: section) + self.tableView?.reloadSections(indexSet, withRowAnimation: animation) - switch reloadType { - case .DeleteRows(let animation): - let indexPaths = safeRange.map { NSIndexPath(forRow: $0, inSection: section) } - self.tableView?.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: animation) - - case .ReloadSection(let animation): - let indexSet = NSIndexSet(index: section) - self.tableView?.reloadSections(indexSet, withRowAnimation: animation) + case .None: + break - case .None: - break - - default: - self.tableView?.reloadData() - } + default: + self.tableView?.reloadData() } - //} + } } } // MARK - Update user info public extension MYTableViewManager { func updateUserData(userData: AnyObject?, inSection section: Int, atRow row: Int, reloadType: MYReloadType = .ReloadRows(.None)) { - //dispatch_async(dispatch_get_main_queue()) { - if self.dataSource[section] != nil { - if let data = self.dataSource[section]?.get(row) { - data.userData = userData - data.calculatedHeight = nil + if self.dataSource[section] != nil { + if let data = self.dataSource[section]?.get(row) { + data.userData = userData + data.calculatedHeight = nil - switch reloadType { - case .ReloadRows(let animation): - let indexPath = NSIndexPath(forRow: row, inSection: section) - self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: animation) + switch reloadType { + case .ReloadRows(let animation): + let indexPath = NSIndexPath(forRow: row, inSection: section) + self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: animation) - case .ReloadSection(let animation): - let indexSet = NSIndexSet(index: section) - self.tableView?.reloadSections(indexSet, withRowAnimation: animation) + case .ReloadSection(let animation): + let indexSet = NSIndexSet(index: section) + self.tableView?.reloadSections(indexSet, withRowAnimation: animation) - case .None: - break + case .None: + break - default: - self.tableView?.reloadData() - } + default: + self.tableView?.reloadData() } } - //} + } } } @@ -340,21 +326,21 @@ public extension MYTableViewManager { // MARK - header/footer public extension MYTableViewManager { - public func setHeaderData(data: MYHeaderFooterViewData, inSection section: Int) { + func setHeaderData(data: MYHeaderFooterViewData, inSection section: Int) { headerViewData[section] = data } - public func setFooterData(data: MYHeaderFooterViewData, inSection section: Int) { + func setFooterData(data: MYHeaderFooterViewData, inSection section: Int) { footerViewData[section] = data } - public func setHeaderViewInSection(section: Int, hidden: Bool) { + func setHeaderViewInSection(section: Int, hidden: Bool) { if let data = headerViewData[section] { data.isEnabled = !hidden } } - public func setFooterViewInSection(section: Int, hidden: Bool) { + func setFooterViewInSection(section: Int, hidden: Bool) { if let data = footerViewData[section] { data.isEnabled = !hidden } @@ -363,12 +349,12 @@ public extension MYTableViewManager { // MARK - private methods private extension MYTableViewManager { - private func addSelectedView(view: MYBaseViewProtocol) { + func addSelectedView(view: MYBaseViewProtocol) { deselectAllCells() selectedCells = [view] } - private func setBaseViewDataDelegate(dataList: [MYBaseViewData]) { + func setBaseViewDataDelegate(dataList: [MYBaseViewData]) { for data in dataList { data.delegate = self }