ROGenericTableViewController 2.1.0

ROGenericTableViewController 2.1.0

测试已测试
Lang语言 SwiftSwift
许可 MIT
发布最后发布2016年9月
SPM支持 SPM

Robin Oster 维护。



  • Robin Oster

ROGenericTableViewController

一个通用的 TableViewController 类,可以轻松处理自定义创建的对象

如何使用

使用方法非常简单。包含 ROGenericTableViewController.swift,而不是实现不同的 UITableViewController Delegate 方法,使用 createGenericViewController 方法创建一个 UITableViewController。

自定义创建的对象可以是任何东西。ROGenericTableViewController 类不需要知道你传递了哪个对象。

class Company {
    var name:String = ""
    var amountWorkers:Int = 0

    init(_ name:String, _ amountWorkers:Int) {
        self.name = name
        self.amountWorkers = amountWorkers
    }
}

这是显示在 UITableView 中的自定义创建类的完整代码。

      override func viewDidLoad() {
        super.viewDidLoad()

        var glencore = Company("Glencore", 10000)
        var nestle = Company("Nestle", 80011)

        var cellForRow = ({ (tableView:UITableView, company:Company) -> UITableViewCell in
            // CellForRowAtIndexPath
            var customCell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell?

            if customCell == nil {
                customCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") as UITableViewCell
            }

            customCell!.textLabel?.text = company.name
            customCell!.detailTextLabel?.text = "Amount of workers: \(company.amountWorkers)"

            return customCell!
        })

        var didCellSelect = ({(company:Company) -> () in
            println("Company: \(company.name) got selected")
        })

        var tableViewController = createViewControllerGeneric([nestle, glencore], cellForRow, didCellSelect) as! ROGenericTableViewController

        // Add the swipe actions to the tableViewController
        tableViewController.swipeActions = createSwipeActions()

        self.viewControllers = [tableViewController]
    }

    func createSwipeActions() -> [UITableViewRowAction] {
        var favAction = UITableViewRowAction(style: .Normal, title: "Fire all employees") { (action, indexPath) -> Void in
            println("Fire everyone from the company")
        }

        favAction.backgroundColor = UIColor.brownColor()

        return [favAction]
    }

许可

The MIT License (MIT)

Copyright (c) 2015 Robin Oster (http://prine.ch)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

作者

Robin Oster,[email protected]