Skip to content

Commit

Permalink
Swift 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Sep 3, 2015
1 parent 2815532 commit 0b328e4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Example/MYTableViewManager/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ViewController: UIViewController {
let cellmodels0 = (0..<2).map { [weak self] i -> MYCellModel in
let title = "Section 0 : index \(i)"
return CustomCellModel(title: title) { _ in
println("Did select new cell : \(i)")
print("Did select new cell : \(i)")
self?.pushChildViewController()
}
}
Expand All @@ -47,7 +47,7 @@ class ViewController: UIViewController {

let cellmodels1 = titles.map { [weak self] title -> MYCellModel in
let data = CustomCellModel(title: "Section 1: " + title) { _ in
println("Did select cell with title = \(title)")
print(")Did select cell with title = \(title)")
self?.pushChildViewController()
}
data.dynamicHeightEnabled = true
Expand Down
8 changes: 4 additions & 4 deletions Source/Hakuba.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class Hakuba : NSObject {
if let s = sections.my_get(index) {
return s
}
let length = index + 1 - sectionCount
//let length = index + 1 - sectionCount

let newSections = (sectionCount...index).map { i -> MYSection in
let ns = MYSection()
Expand Down Expand Up @@ -142,7 +142,7 @@ public extension Hakuba {
return self
}

func slide(_ animation: MYAnimation = .None) -> Self {
func slide(animation: MYAnimation = .None) -> Self {
// TODO : implementation
tableView?.reloadData()
insertedSectionsRange = (100, -1)
Expand Down Expand Up @@ -373,8 +373,8 @@ extension Hakuba {

public func scrollViewDidScroll(scrollView: UIScrollView) {
delegate?.scrollViewDidScroll?(scrollView)
if let indexPath = tableView?.indexPathsForVisibleRows()?.first as? NSIndexPath {
if let indexPath = tableView?.indexPathsForVisibleRows?.first {
if currentTopSection != indexPath.section {
if let headerView = tableView?.headerViewForSection(currentTopSection) as? MYHeaderFooterView {
headerView.didChangeFloatingState(false)
Expand Down
2 changes: 1 addition & 1 deletion Source/MYCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public typealias MYSelectionHandler = (MYBaseViewProtocol) -> ()
public typealias MYAnimation = UITableViewRowAnimation

public protocol MYBaseViewProtocol {
func emitSelectedEvent(MYBaseViewProtocol)
func emitSelectedEvent(_: MYBaseViewProtocol)
}

public protocol MYBaseViewDelegate : class {
Expand Down
13 changes: 3 additions & 10 deletions Source/MYExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,14 @@ extension Array {
let mIndex = max(0, index)
return min(count, mIndex)
}

func my_indexOf<T: Equatable>(item: T) -> Int? {
if item is Element {
return find(unsafeBitCast(self, [T].self), item)
}
return nil
}


func my_getSafeRange(range: Range<Int>) -> Range<Int>? {
let start = max(0, range.startIndex)
let end = min(count, range.endIndex)
return start <= end ? Range<Int>(start: start, end: end) : nil
}

func my_get(index: Int) -> T? {
func my_get(index: Int) -> Element? {
return my_hasIndex(index) ? self[index] : nil
}

Expand Down Expand Up @@ -139,7 +132,7 @@ extension Array {
}

func my_each(exe: (Int, Element) -> ()) {
for (index, item) in enumerate(self) {
for (index, item) in enumerate() {
exe(index, item)
}
}
Expand Down
19 changes: 7 additions & 12 deletions Source/MYHeaderFooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ public class MYHeaderFooterView : UITableViewHeaderFooterView, MYBaseViewProtoco
private weak var delegate: MYBaseViewDelegate?
public var selectable = true

public required init(coder aDecoder: NSCoder) {
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}


public override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
setup()
Expand Down Expand Up @@ -77,21 +72,21 @@ public extension MYHeaderFooterView {

// MARK - Touch events
public extension MYHeaderFooterView {
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
if selectable {
highlight(false)
}
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
if selectable {
unhighlight(false)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
if selectable {
emitSelectedEvent(self)
Expand Down
2 changes: 1 addition & 1 deletion Source/MYReloadTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MYReloadTracker {
originalIndexes.my_remove(range)
for i in ri {
var newIndexes: [Int] = []
if removedIndexes.my_indexOf(i) == nil {
if removedIndexes.indexOf(i) == nil {
newIndexes.append(i)
}
removedIndexes += newIndexes
Expand Down
4 changes: 2 additions & 2 deletions Source/MYSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public extension MYSection {
}

func remove(cellmodel: MYCellModel) -> Self {
if let index = find(items, cellmodel) {
if let index = items.indexOf(cellmodel) {
return remove(index)
}
return self
}

// MARK - slide
func slide(_ animation: MYAnimation = .None) -> Self {
func slide(animation: MYAnimation = .None) -> Self {
switch reloadTracker.state {
case .Add:
delegate?.insertRows(reloadTracker.getIndexPaths(index), animation: animation)
Expand Down
8 changes: 4 additions & 4 deletions Source/MYTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public class MYCellModel : MYViewModel {
self.height = height
}

public func slide(_ animation: MYAnimation = .None) -> Self {
public func slide(animation: MYAnimation = .None) -> Self {
delegate?.reloadView(row, section: section, animation: animation)
return self
}
}

public class MYTableViewCell : UITableViewCell, MYBaseViewProtocol {
private weak var delegate: MYBaseViewDelegate?
weak var cellModel: MYCellModel?
public required init(coder aDecoder: NSCoder) {
public internal(set) weak var cellModel: MYCellModel?
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
Expand Down

0 comments on commit 0b328e4

Please sign in to comment.