Skip to content

Instantly share code, notes, and snippets.

@lifely
Last active August 29, 2015 14:22
Show Gist options
  • Save lifely/d01c2b7425c09ecc7add to your computer and use it in GitHub Desktop.
Save lifely/d01c2b7425c09ecc7add to your computer and use it in GitHub Desktop.
UIStaticTableViewController
//
// HamakDynamicController.swift
// Hamak
//
// Created by Julien Di Marco on 03/06/15.
// Copyright (c) 2015 SoLocal. All rights reserved.
//
import Foundation
class UIStaticTableViewController: UITableViewController {
// MARK: - Properties -
private var tableViewRepresentation: [[UITableViewCell]]? = nil
// MARK: - Computed Properties -
private var indexPathRepresentation: [[NSIndexPath]]? {
let array_: [[UITableViewCell]]! = self.tableViewRepresentation
if (array_ == nil) { return nil }
let representation_: [[NSIndexPath]] = array_.map() { section in
let sectionIndex_ = find(array_ as Array, section) ?? 0
let indexPath_ = section.map() {
NSIndexPath(forRow: find(section, $0) ?? 0, inSection: sectionIndex_) ?? NSIndexPath(forRow: 0, inSection: 0)!
}
return indexPath_.filter() { array_[$0.section][$0.row].tag == 1 ? false : true }
}
return representation_
}
// MARK: - Life cycle -
override func awakeFromNib() {
super.awakeFromNib()
self.tableViewRepresentation = self.representationOfTableView(self.tableView)
}
// MARK: - Helper -
func representationOfTableView(tableView: UITableView) -> [[UITableViewCell]]?
{
let cells_: [UITableViewCell]! = tableView.visibleCells() as? [UITableViewCell]
let nbOfSection_ = tableView.numberOfSections()
if (cells_ == nil) { return []; }
var representation_: [[UITableViewCell]] = Array<[UITableViewCell]>(count: nbOfSection_, repeatedValue: []);
for (var j = 0; j < nbOfSection_; j++) {
let nbOfRows_ = tableView.numberOfRowsInSection(j)
for (var i = 0; i < nbOfRows_; i++) {
let cell_ = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: j)) ?? UITableViewCell()
representation_[j].insert(cell_, atIndex: i)
}
}
return representation_
}
// MARK: - Delegates
// MARK: - UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let section_: [NSIndexPath]! = self.indexPathRepresentation?[section]
if (section_ == nil) { return super.tableView(tableView, numberOfRowsInSection: section) }
return section_.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let index = self.indexPathRepresentation?[indexPath.section][indexPath.row]
let cell_ = super.tableView(tableView, cellForRowAtIndexPath: index ?? indexPath)
//println("cellForRowAtIndexPath:(\(indexPath.row), \(indexPath.section)) - cell: \(cell_)")
return cell_
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment