ImoTableView 1.0.25

ImoTableView 1.0.25

Borinschi Ivan 维护。



alt text

围绕 UITableView 的封装,旨在简化表格的交互。

入门指南

为了开始使用 ImoTableView,您需要了解其基础概念,ImoTableView 由四个类组成。

  1. ImoTableViewUITableView 的子类,负责管理 ImoTableViewSection,添加新分区、删除和更新它们。
  2. ImoTableViewSection 是一个类,与 ImoTableViewSource 一起工作,添加新源、删除和更新。
  3. ImoTableViewSource 是您新的 CellSource 的基类,包含有关单元格类的基信息、单元格高度以及您想在单元格中显示的其他附加属性,例如用户名。
  4. ImoTableViewCell 是属性的视图表示形式,存储在您的来源中。

alt text

实现示例

以下是如何快速简单地添加和填充 tableView 的示例。这是 Swift 项目的 ViewController.swift 看起来的样子:

//Create an table andd ad on SomeUIView
let tableView = ImoTableView(on: someUIView, insets: UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0))
//Create new section
let section = ImoTableViewSection()
//Create new cellSource
let actionCellSource = ActionCellSource(title: "Action")
//Add cellSource to section
section.add(actionCellSource)
//Add section to table View
tableView.add(section)
//Reload table
tableView.reloadData()

ActionCell.swift 包含一个方法 swift open override func setUpWithSource(source:AnyObject),每次 ActionCell 显示在屏幕上都会调用该方法,并且 swift source:AnyObject 是包含所有所需设置的单元格属性的单元格源。

open class ActionCell: ImoTableViewCell {
    //UILabel from ActionCell.xib
    @IBOutlet weak var actionTitle: UILabel!
    //This method is called every time your cell will be displayed on screen
    open override func setUpWithSource(source: AnyObject) {
        //Cast source to ActionCellSource
        if let source = source as? ActionCellSource {
            //Set the label title
            self.actionTitle.text = source.title
        }
    }
}

ActionCellSource.swift 包含一个方法 swift open override func setUpWithSource(source:AnyObject),每次 ActionCell 显示在屏幕上都会调用该方法,并且 swift source:AnyObject 是包含所有所需设置的单元格属性的单元格源。

open class ActionCellSource: ImoTableViewSource {

    public var title: String

    init(title: String) {
        self.title = title
        //Init source an specify cell class you will represent by this source
        super.init(cellClass: "ActionCell")
        //Set nib bundle if your cell is not in current project bundle
        setNibBundle(with:Bundle.init(for: self.classForCoder))
    }
}

模板

为快速使用 ImoTableView,您需要将 ImoTableViewCell.xctemplate 添加到您的 Xcode 模板中。这个模板将创建创建新 CellCellSource 所需的全部内容。

alt text

要将 ImoTableViewCell.xctemplate.xctemplate 添加到 Xcode 中,您需要打开文件模板文件夹,您可以通过以下终端命令轻松地完成操作:open /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File\ Templates/

在创建名为 ImoTableView 的文件夹后,将此文件夹中的 ImoTableViewCell.xctemplate 从本仓库的 Templates/ 文件夹中复制进去。

代码片段

ImoTableViewSection

//Create new section
let section = ImoTableViewSection()

//Add CellSource
section.add(yourCellSource)

//Delete CellSource
section.delete(atIndex: yourCellIndex)

//Delete CellSource
section.delete(yourCellSource)

//Delete All
section.deleteAll()

//Set section header View
section.headerView = YourView

//Set section footer View
section.footerView = YourView

ImoTableView

//Create new table
let table = ImoTableView()

//Add section
table.add(yourSection)

//Add sections
table.add(yourSections)

//Delete section at index
table.deleteSection(at: yourSectionIndex)

//Delete all sections
table.deleteAllSections()

//Did select source, after user touch up on cell
table.didSelectSource = { source in
    //Do something with source
}

//Did select cell at index path, after user touch up on cell
table.didSelectCellAtIndexPath = { indexPath in
    //Do something with source
}

要求

  • iOS 8.0+
  • Xcode 8.1+
  • Swift 3.1+

安装

Carthage

Carthage 是一个去中心化的依赖管理工具,它构建您的依赖并提供二进制框架。

您可以使用以下命令通过 Homebrew 安装 Carthage:

$ brew update
$ brew install carthage

要使用 Carthage 将 ImoTableView 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它。

github "imodeveloperlab/ImoTableView" ~> 1.0

运行 carthage update 以构建框架,并将构建好的 ImoTableView.framework 拖放到您的 Xcode 项目中。

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令进行安装

$ gem install cocoapods

CocoaPods 1.1.0+ 是构建 ImoTableView 1.0.8+ 必须的。

要使用 CocoaPods 将 ImoTableView 集成到您的 Xcode 项目中,请在 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'ImoTableView', '~> 1.0.8'
end

然后,运行以下命令

$ pod install

示例

基本示例

//TABLE
let table = ImoTableView(on: self.view)

//SECTION
let section = ImoTableViewSection()

//FAKER
let faker = Faker.init()

let firstName = TextCellSource(text: "First Name: \(faker.name.firstName())")
section.add(firstName)

let lastName = TextCellSource(text: "Last Name: \(faker.name.lastName())")
section.add(lastName)

let company = TextCellSource(text: "Company: \(faker.company.name())")
section.add(company)

table.add(section: section)

alt text

基本动作示例

override func viewDidLoad() {

        super.viewDidLoad()

        //TABLE
        let table = ImoTableView(on: self.view)

        //SECTION
        let section = ImoTableViewSection()

        //FAKER
        let faker = Faker.init()

        //1. First way
        let appNameCell = ActionCellSource(title: "App name: \(faker.app.name())")
        section.add(appNameCell, target: self, #selector(didSelectAppName))

        //2. Second way
        let appAuthorCell = ActionCellSource(title: "Author: \(faker.app.author())")
        appAuthorCell.target = self
        appAuthorCell.selector = #selector(didSelectAppAuthor)
        section.add(appAuthorCell)

        //3. Third way
        let appVersion = faker.app.version()
        let appVersionCell = ActionCellSource(title: "App version: \(appVersion)")
        appVersionCell.target = self
        appVersionCell.object = appVersion as AnyObject
        appVersionCell.selector = #selector(didSelectAppVersion)
        section.add(appVersionCell)

        table.add(section: section)
}

func didSelectAppVersion(sender: AnyObject) {

    let version = sender as! String
    show(message: "Did select version \(version)")
}

func didSelectAppAuthor() {

    self.show(message: "Did select app author")
}

func didSelectAppName() {

    show(message: "Did select app name")
}

alt text

多节示例

//TABLE
let table = ImoTableView(on: self.view)

//USER INFO SECTION
let userInfoSection = ImoTableViewSection()
userInfoSection.headerTitle = "User info"

let firstName = TextCellSource(text: "First Name: \(faker.name.firstName())")
userInfoSection.add(firstName)

let lastName = TextCellSource(text: "Last Name: \(faker.name.lastName())")
userInfoSection.add(lastName)
table.add(section: userInfoSection)

//BUSSINES INFO SECTION
let bussinesInfoSection = ImoTableViewSection()
bussinesInfoSection.headerTitle = "Bussines info"

let company = TextCellSource(text: "Company: \(faker.company.name())")
bussinesInfoSection.add(company)

let cardNumber = TextCellSource(text: "Credit Card: \(faker.business.creditCardNumber())")
bussinesInfoSection.add(cardNumber)

let cardType = TextCellSource(text: "Type: \(faker.business.creditCardType())")
bussinesInfoSection.add(cardType)
table.add(section: bussinesInfoSection)

//Lorem section
let loremSection = ImoTableViewSection()
loremSection.headerTitle = "Lorem Ipsum"

for _ in 0...20 {
    let text = faker.lorem.sentence()
    loremSection.add(TextCellSource(text: text))
}

table.add(section: loremSection)

alt text

动画添加单元格示例

override func viewDidLoad() {

  super.viewDidLoad()

  //HIDE THE KAYBOARD WHEN TAPPED ARROUND
  self.hideKeyboardWhenTappedAround()

  //TABLE
  self.table = ImoTableView(on: self.view)

  //ADD ONE CELL ACTION
  let addSource = ActionCellSource(title: "Add new Cell")
  mainSection.add(addSource, target: self, #selector(addNewCell))

  //ADD MULTIPLE CELL'S ACTION
  let addMultipleSource = ActionCellSource(title: "Add multiple Cells")
  mainSection.add(addMultipleSource, target: self, #selector(addMultipleCells))

  //HEADER TITLE
  secondSection.headerTitle = "Cells"

  //ADD SECTIONS TO TABLE
  table.add(section: mainSection)
  table.add(section: secondSection)
}

func addNewCell() {

  let text = faker.lorem.sentence()
  let source = TextCellSource(text: text)
  table.add(source: source, in: secondSection, animated: true, animation: .top)
}

func addMultipleCells() {

  var sources:[TextCellSource] = []

  for _ in 0...10 {
      let text = faker.lorem.sentence()
      sources.append(TextCellSource(text: text))
  }

  table.add(sources: sources, in: secondSection, animated: true, animation: .top)
}

alt text

动画删除单元格示例

override func viewDidLoad() {

    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    self.table = ImoTableView(on: self.view)

    let deleteFirstSource = ActionCellSource(title: "Delete first cell")
    mainSection.add(deleteFirstSource, target: self, #selector(deleteFirst))

    let deleteLastSource = ActionCellSource(title: "Delete last cell")
    mainSection.add(deleteLastSource, target: self, #selector(deleteLast))

    let deleteAllSource = ActionCellSource(title: "Delete all cells")
    mainSection.add(deleteAllSource, target: self, #selector(deleteAll))

    secondSection.headerTitle = "Cells"

    addMultipleCells()

    table.add(section: mainSection)
    table.add(section: secondSection)
}

func addMultipleCells() {

    var sources:[TextCellSource] = []

    for _ in 0...5 {
        let text = faker.lorem.sentence()
        sources.append(TextCellSource(text: text))
    }

    secondSection.add(sources: sources)
}

func deleteFirst()  {

    if let source = secondSection.firstSource() {
        table.delete(source: source, in: secondSection, animated: true, animation: .left)
    }
}

func deleteLast()  {

    if let source = secondSection.lastSource() {
        table.delete(source: source, in: secondSection, animated: true, animation: .right)
    }
}

func deleteAll()  {

    let allSources = secondSection.allSources()
    table.delete(sources: allSources, in: secondSection, animated: true, animation: .top)
}

alt text

动画删除单元格示例

self.hideKeyboardWhenTappedAround()
let table = ImoTableView(on: self.view, insets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))

table.tableView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 60, right: 0)
table.tableView.scrollIndicatorInsets = UIEdgeInsets(top: 100, left: 0, bottom: 60, right: 0)

table.adjustContentInsetsForKeyboard(true)
let section = ImoTableViewSection()

for _ in 0...5 {
    let textField = TextFieldCellSource(staticCellWithTableView: table)
    section.add(textField)
}

for _ in 0...20 {
    let text = faker.lorem.sentence()
    section.add(TextCellSource(text:text))
}

table.add(section: section)

alt text