Giter Site home page Giter Site logo

easysections's Introduction

EasySections

Tiny UITableView wrapper for easier handling of table view sections.

Using EasySections, you can declare your table view sections implementing the already familiar UITableViewDelegate and UITableViewDataSource methods. Then you can easily mix these sections in your implementation. Here's how that looks like: You make a subclass of AbstractSectionDelegate for each section in your table view. Let's say you want a section that has blue cells:

class BlueSectionDelegate: AbstractSectionDelegate {

var array: [Int]

init(_ array: [Int]) {
  self.array = array
  super.init()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return self.array.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  var blueCell = tableView.dequeueReusableCell(withIdentifier: "blueCell")
    if blueCell == nil {
    blueCell = UITableViewCell.init(style: .default, reuseIdentifier: "blueCell")
  }
  blueCell?.backgroundColor = .blue
  blueCell?.textLabel?.text = String(array[indexPath.row])
  return blueCell!
  }
}

And another one which has red cells:

class RedSectionDelegate: AbstractSectionDelegate {

var array: [String]

init(_ array: [String]) {
  self.array = array
  super.init()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return array.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  var redCell = tableView.dequeueReusableCell(withIdentifier: "redCell")
  if redCell == nil {
    redCell = UITableViewCell.init(style: .default, reuseIdentifier: "redCell")
  }
  redCell?.backgroundColor = .red
  redCell?.textLabel?.text = array[indexPath.row]
  return redCell!
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  return CGFloat((indexPath.row + 1) * 40)
  }
}

And then in your controller just do:

redSectionDelegate = RedSectionDelegate.init(["Should", "I", "Stay", "Or", "Should", "I", "go"])
blueSectionDelegate = BlueSectionDelegate.init([10, 20, 30, 40, 50])
let delegates: [AbstractSectionDelegate] = [redSectionDelegate, blueSectionDelegate]
tableViewDelegate = MainTableViewDelegate.init(delegates)
tableView.delegate = tableViewDelegate
tableView.dataSource = tableViewDelegate
tableView.reloadData()

easysections's People

Contributors

andrejtrajkovski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.