Skip to content

Commit

Permalink
Merge branch 'newdesign'
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Feb 15, 2015
2 parents 9c57e29 + adaf027 commit 51efd65
Show file tree
Hide file tree
Showing 11 changed files with 617 additions and 357 deletions.
20 changes: 16 additions & 4 deletions Example/MYTableViewManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
86398C851A6569AC0014C50F /* MYBaseClasses.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86398C841A6569AC0014C50F /* MYBaseClasses.swift */; };
86398C851A6569AC0014C50F /* MYCommon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86398C841A6569AC0014C50F /* MYCommon.swift */; };
86398C871A6569BC0014C50F /* MYTableViewManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86398C861A6569BC0014C50F /* MYTableViewManager.swift */; };
86398C891A6569CC0014C50F /* MYTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86398C881A6569CC0014C50F /* MYTableViewCell.swift */; };
86398C8B1A6569D90014C50F /* MYHeaderFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86398C8A1A6569D90014C50F /* MYHeaderFooterView.swift */; };
Expand All @@ -22,6 +22,9 @@
86B1FFE51A6568F1000E3772 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 86B1FFE41A6568F1000E3772 /* Images.xcassets */; };
86B1FFE81A6568F1000E3772 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 86B1FFE61A6568F1000E3772 /* LaunchScreen.xib */; };
86B1FFF41A6568F1000E3772 /* MYTableViewManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86B1FFF31A6568F1000E3772 /* MYTableViewManagerTests.swift */; };
8C94F4411A907B9F00B2B2D0 /* MYReloadTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C94F4401A907B9F00B2B2D0 /* MYReloadTracker.swift */; };
8C9CD6FC1A8F4EBB004802C7 /* MYSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C9CD6FB1A8F4EBB004802C7 /* MYSection.swift */; };
8C9CD6FE1A8F4F27004802C7 /* MYViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C9CD6FD1A8F4F27004802C7 /* MYViewModel.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -35,7 +38,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
86398C841A6569AC0014C50F /* MYBaseClasses.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYBaseClasses.swift; sourceTree = "<group>"; };
86398C841A6569AC0014C50F /* MYCommon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYCommon.swift; sourceTree = "<group>"; };
86398C861A6569BC0014C50F /* MYTableViewManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYTableViewManager.swift; sourceTree = "<group>"; };
86398C881A6569CC0014C50F /* MYTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYTableViewCell.swift; sourceTree = "<group>"; };
86398C8A1A6569D90014C50F /* MYHeaderFooterView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYHeaderFooterView.swift; sourceTree = "<group>"; };
Expand All @@ -54,6 +57,9 @@
86B1FFED1A6568F1000E3772 /* MYTableViewManagerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MYTableViewManagerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
86B1FFF21A6568F1000E3772 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
86B1FFF31A6568F1000E3772 /* MYTableViewManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MYTableViewManagerTests.swift; sourceTree = "<group>"; };
8C94F4401A907B9F00B2B2D0 /* MYReloadTracker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYReloadTracker.swift; sourceTree = "<group>"; };
8C9CD6FB1A8F4EBB004802C7 /* MYSection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYSection.swift; sourceTree = "<group>"; };
8C9CD6FD1A8F4F27004802C7 /* MYViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MYViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -77,7 +83,10 @@
86398C831A6569810014C50F /* Source */ = {
isa = PBXGroup;
children = (
86398C841A6569AC0014C50F /* MYBaseClasses.swift */,
86398C841A6569AC0014C50F /* MYCommon.swift */,
8C94F4401A907B9F00B2B2D0 /* MYReloadTracker.swift */,
8C9CD6FD1A8F4F27004802C7 /* MYViewModel.swift */,
8C9CD6FB1A8F4EBB004802C7 /* MYSection.swift */,
86398C861A6569BC0014C50F /* MYTableViewManager.swift */,
86398C881A6569CC0014C50F /* MYTableViewCell.swift */,
86398C8A1A6569D90014C50F /* MYHeaderFooterView.swift */,
Expand Down Expand Up @@ -249,15 +258,18 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8C94F4411A907B9F00B2B2D0 /* MYReloadTracker.swift in Sources */,
86398C8D1A6569EB0014C50F /* MYLabel.swift in Sources */,
86398C931A656AD40014C50F /* ChildViewController.swift in Sources */,
86398C891A6569CC0014C50F /* MYTableViewCell.swift in Sources */,
86B1FFE01A6568F1000E3772 /* ViewController.swift in Sources */,
86398C851A6569AC0014C50F /* MYBaseClasses.swift in Sources */,
86398C851A6569AC0014C50F /* MYCommon.swift in Sources */,
86398C911A656AAC0014C50F /* CustomCell.swift in Sources */,
86398C8F1A6569F70014C50F /* MYExtension.swift in Sources */,
86B1FFDE1A6568F1000E3772 /* AppDelegate.swift in Sources */,
86398C8B1A6569D90014C50F /* MYHeaderFooterView.swift in Sources */,
8C9CD6FC1A8F4EBB004802C7 /* MYSection.swift in Sources */,
8C9CD6FE1A8F4F27004802C7 /* MYViewModel.swift in Sources */,
86398C871A6569BC0014C50F /* MYTableViewManager.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion Example/MYTableViewManager/CustomCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CustomCell : MYTableViewCell {
override func configureCell(data: MYCellViewModel) {
super.configureCell(data)
if let title = data.userData as? String {
titleLabel.text = title
titleLabel.text = title + "(\(data.section),\(data.row))"
}
}
}
110 changes: 84 additions & 26 deletions Example/MYTableViewManager/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,60 @@ import UIKit

class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
private var tableViewManager: MYTableViewManager!
private var tvm: MYTableViewManager!

override func viewWillAppear(animated: Bool) {
tableViewManager?.deselectAllCells()
tvm?.deselectAllCells()
}

override func viewDidLoad() {
super.viewDidLoad()
tableViewManager = MYTableViewManager(tableView: tableView)
tableViewManager.delegate = self
tvm = MYTableViewManager(tableView: tableView)
tvm.delegate = self

tableViewManager.registerCellNib(CustomCell)
tvm.registerCellNib(CustomCell)
delay(1) {
_ = self.start()
}
}

func start() {

for index in 0...4 {
let cvm = (0..<2).map { [weak self] i -> MYCellViewModel in
return MYCellViewModel(cellClass: CustomCell.self, userData: "index \(index*2 + i)") { _ in
println("Did select new cell : \(index + i)")
self?.pushChildViewController()
}
}
tvm[0].insert(cvm[0], atIndex: 0)
//.fire(.Left)
tvm[0].append(cvm[1])

//tvm[0][index*2]?.userData = "new title: \(index)"
//tvm[0][index + 1]?.fire()
}
//tvm[0].remove(0)

/*
delay(2) {
for index in 0...4 {
self.tvm[0].remove(index+1)
//.fire(.Right)
}
}
*/

println("finish setting")
delay(2) {
self.tvm[0].fire()
return
}

delay(7) {
//self.tvm[0].fire()
return
}
/*
let longTitle1 = "Don't have to write the code for UITableViewDelegate and UITableViewDataSource protocols"
let longTitle2 = "Support dynamic cell height from ios7"
Expand All @@ -37,34 +78,41 @@ class ViewController: UIViewController {
data.dynamicHeightEnabled = true
return data
}
tableViewManager.resetWithData(cellData, inSection: 0)

tableViewManager.loadmoreHandler = { [weak self] in
println("Loadmore")
self?.delay(1) {
self?.tableViewManager.loadmoreEnabled = true
return
}
}

delay(1.0) {
self.tvm[0].reset(cellData)
.fire()

delay(2) {
let titles = ["new cell 1", "new cell 2"]
let newCellData = titles.map { [weak self] title -> MYCellViewModel in
return MYCellViewModel(cellClass: CustomCell.self, userData: title) { _ in
println("Did select new cell : \(title)")
self?.pushChildViewController()
}
}
self.tableViewManager.resetWithData(newCellData, inSection: 5)
//self.tableViewManager.insertData(newCellData, inSection: 0, atRow: 1, reloadType: .InsertRows(.Middle))
self.tvm[0].insert(newCellData, atIndex: 2)
.fire(.Middle)
}

delay(2.0) {
self.tableViewManager.removeDataInSection(0, atRow: 2)

delay(5) {
self.tvm[0].remove(1)
.fire(.Left)
return
}
*/
/*
tvm.loadmoreHandler = { [weak self] in
println("Loadmore")
self?.delay(1) {
self?.tvm.loadmoreEnabled = true
return
}
}



delay(3.0) {
self.tableViewManager.updateUserData("Last cell", inSection: 5, atRow: 1)
self.tvm.updateUserData("Last cell", inSection: 5, atRow: 1)
}

delay(5.0) {
Expand All @@ -75,18 +123,29 @@ class ViewController: UIViewController {
self?.pushChildViewController()
}
}
self.tableViewManager.insertDataBeforeLastRow(newCellData, inSection: 0, reloadType: .InsertRows(.Middle))
self.tvm.insertDataBeforeLastRow(newCellData, inSection: 0, reloadType: .InsertRows(.Middle))
}

delay(6.0) {
self.tableViewManager.removeLastDataInSection(0)
//self.tableViewManager.removeDataInSection(0, inRange: (7..<9), reloadType: .DeleteRows(.Middle))
self.tvm.removeLastDataInSection(0)
//self.tvm.removeDataInSection(0, inRange: (7..<9), reloadType: .DeleteRows(.Middle))
}

tableViewManager.loadmoreEnabled = true
tvm.loadmoreEnabled = true
*/
}

func appendItems(index: Int, num: Int) {
let cvm = (0..<num).map { [weak self] i -> MYCellViewModel in
return MYCellViewModel(cellClass: CustomCell.self, userData: "index \(index + i)") { _ in
println("Did select new cell : \(index + i)")
self?.pushChildViewController()
}
}
self.tvm[0].append(cvm)
.fire(.Left)
}

func pushChildViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ChildViewController") as ChildViewController
Expand All @@ -105,7 +164,6 @@ class ViewController: UIViewController {

extension ViewController : MYTableViewManagerDelegate {
func scrollViewDidScroll(scrollView: UIScrollView) {
println("OK")
}
}

22 changes: 22 additions & 0 deletions Source/MYCommon.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// MYCommon.swift
// MYTableViewManager
//
// Created by Le Van Nghia on 1/13/15.
// Copyright (c) 2015 Le Van Nghia. All rights reserved.
//

import UIKit

public typealias MYSelectionHandler = (MYBaseViewProtocol) -> ()
public typealias MYAnimation = UITableViewRowAnimation

public protocol MYBaseViewProtocol {
func highlight(Bool)
func unhighlight(Bool)
func emitSelectedEvent(MYBaseViewProtocol)
}

public protocol MYBaseViewDelegate : class {
func didSelect(view: MYBaseViewProtocol)
}
74 changes: 69 additions & 5 deletions Source/MYExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,78 @@ extension String {
}
}

extension NSRange {
init(range: Range<Int>) {
self.location = range.startIndex
self.length = range.endIndex - range.startIndex
}
}

extension Array {
mutating func insert(newArray: Array, atIndex index: Int) {
let left = self[0..<max(0, index)]
let right = index > count ? [] : self[index..<count]
self = left + newArray + right
func hasIndex(index: Int) -> Bool {
return index >= 0 && index < count
}

func getSafeIndex(index: Int) -> Int {
return min(count, max(0, index))
}

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

func 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 get(index: Int) -> T? {
return 0 <= index && index < count ? self[index] : nil
return hasIndex(index) ? self[index] : nil
}

mutating func append(newArray: Array) -> Range<Int> {
let range = Range<Int>(start: count, end: count + newArray.count)
self += newArray
return range
}

mutating func insert(newArray: Array, atIndex index: Int) -> Range<Int> {
let start = min(count, max(0, index))
let end = start + newArray.count

let left = self[0..<start]
let right = self[start..<count]
self = left + newArray + right
return Range<Int>(start: start, end: end)
}

mutating func remove(index: Int) -> Range<Int>? {
if !hasIndex(index) {
return nil
}
self.removeAtIndex(index)
return Range<Int>(start: index, end: index + 1)
}

mutating func remove(range: Range<Int>) -> Range<Int>? {
if let sr = getSafeRange(range) {
self.removeRange(sr)
return sr
}
return nil
}

mutating func removeLast() -> Range<Int>? {
return self.remove(count - 1)
}

func each(exe: (Int, Element) -> ()) {
for (index, item) in enumerate(self) {
exe(index, item)
}
}
}
15 changes: 13 additions & 2 deletions Source/MYHeaderFooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ import UIKit

public class MYHeaderFooterViewModel : MYViewModel {
let identifier: String
var viewHeight: CGFloat = 44
var isEnabled = true
internal(set) var section: Int = 0
internal(set) var isHeader = true
public var viewHeight: CGFloat = 44
public var isEnabled = true

public init(viewClass: AnyClass, userData: AnyObject?, selectionHandler: MYSelectionHandler? = nil) {
self.identifier = String.className(viewClass)
super.init(userData: userData, selectionHandler: selectionHandler)
}

func fire() -> Self {
if isHeader {
delegate?.reloadHeader(section)
} else {
delegate?.reloadFooter(section)
}
return self
}
}

public class MYHeaderFooterView : UITableViewHeaderFooterView, MYBaseViewProtocol {
Expand Down
Loading

0 comments on commit 51efd65

Please sign in to comment.