Coder Social home page Coder Social logo

mdtable's Introduction

Version Platform Language License

MDTables

An elegant way to create tableView

let row0 = Row(title: "System Cell", accessoryType: .disclosureIndicator)
row0.onDidSelected { (tableView, indexPath) in
    tableView.deselectRow(at: indexPath, animated: true)
}
let row1 = Row(title: "Custom Cell", accessoryType: .disclosureIndicator)
    
let section0 = Section(rows: [row0,row1])
section0.titleForHeader = "Basic"
section0.heightForHeader = 30.0
    
tableView.manager = TableManager(sections: [section0])

And your tableView is ready.

Demo

The example project contains a demo of NeteaseCloudMusic。(以网易云音乐首页作为Demo)

Require

  • Xcode 8.1+
  • iOS 8.0+
  • Swift 3.0+

Install

CocoaPod

pod "MDTable"

Carthage

github "LeoMobileDeveloper/MDTable"

Useage

Basic concept

MDTable offers tow basic types:

  • Row - model of Cell.
  • Section- model of Section

You create rows and sections.

let row = Row(title: "System Cell", accessoryType: .disclosureIndicator)
let section0 = Section(rows: row)

Then use declarative API to handle event

row.onWillDisplay { (tableView, cell, indexPath) in
    //Access manager with tableView.manager
}
row.onDidSelected { (tableView, indexPath) in }

Then,create a manager and bind it to tableView

tableView.manager = TableManager(sections: [section0])

Custom Cell

Model

Create a subClass of ReactiveRow,

class XibRow:ReactiveRow{
    //Data
    var title:String
    var subTitle:String
    var image:UIImage
    init(title:String, subTitle:String, image:UIImage) {
        self.title = title
        self.subTitle = subTitle
        self.image = image
        super.init()
        self.rowHeight = 80.0
        self.reuseIdentifier = "XibRow" //Default reuseIdentifier is class Name
        self.initalType = RowConvertableInitalType.xib(xibName: "CusomCellWithXib") // How row is created
    }

}

Cell

Create a subclass of MDTableViewCell,and override render

class CusomCellWithXib: MDTableViewCell{    
    override func render(with row: TableRow) {
        guard let row = row as? XibRow else{
            return;
        }
        //Render the cell 
    }
}

Magic happens

let row = XibRow(title: "Title", subTitle: "Subtitle", image: UIImage(named: "avatar")!)
row.onDidSelected({ (tableView, indexPath) in
    tableView.manager?.delete(row: indexPath)
    tableView.deleteRows(at: [indexPath], with: .automatic)
})
let section = Section(rows: rows)
section.heightForHeader = 30.0
section.titleForHeader = "Tap Row to Delete"
tableView.manager = TableManager(sections: [section])

Note

You need to use [unowned self] to avoid retain circle

row.onDidSelected = { [unowned self] (tableView, indexPath) in
}

Author

Leo, [email protected]

License

MDTable is available under the MIT license. See the LICENSE file for more info.

mdtable's People

Contributors

leomobiledeveloper avatar

Watchers

James Cloos avatar AFKun 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.