OkDataSources 1.3.3

OkDataSources 1.3.3

测试测试通过
Lang语言 SwiftSwift
许可 Apache-2.0
发布最后发布2018 年 11 月
SPM支持 SPM

Roberto Frontado 维护。



Version License Platform Language

OkDataSources

为 iOS TableView 和 CollectionView DataSource 提供包装,简化了其 API 至最小。而且还具有酷炫的 PagerView 和 SlidingTabs!

设置

将 OkDataSources pod 添加到 podfile 中

pod 'OkDataSources'

对于 RxSwift 扩展,该项目会将其作为依赖项包含其中。因此,您可以通过 CocoaPods 子规格来完成此操作。

pod 'OkDataSources'
pod 'OkDataSources/RxSwift'

OkDataSources 还可通过 Carthage 使用。要安装它,请向您的 Cartfile 添加以下依赖项

github "robertofrontado/OkDataSources"

用法

OkDataSources 提供了几种数据源和代理来处理 iOS TableViews 和 CollectionViews

TableView

class TableViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var dataSource: OkTableViewDataSource<Item, TableViewCell>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dataSource = OkTableViewDataSource()
        tableView.dataSource = dataSource
    }
    
}

看看 OkTableViewDataSource<Item, TableViewCell> 需要一个对象和一个 tableViewCell(该单元必须符合 OkViewCell 协议)

class TableViewCell: UITableViewCell, OkViewCell {
    
    @IBOutlet weak var valueLabel: UILabel!
    
    func configureItem(item: Item) {
        valueLabel.text = item.value
    }
}

Storyboard 中,单元需要具有此类标识符 "\(CLASSNAME)"(例如:TableViewCell

TableViewCell - 1 TableViewCell - 2

如果您想接收关于项目选择的反馈,需要将一个 OkTableViewDelegate 添加到您的 ViewController 中,这样在用户点击项目时,就会调用 onItemClicked 块。

class TableViewController: UIViewController, OkViewCellDelegate {

    @IBOutlet weak var tableView: UITableView!
    var dataSource: OkTableViewDataSource<Item, TableViewCell>!
    var delegate: OkTableViewDelegate<OkTableViewDataSource<Item, TableViewCell>, TableViewController>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dataSource = OkTableViewDataSource()
        delegate = OkTableViewDelegate(dataSource: dataSource,
            onItemClicked: { (item, position) in
            self.showAlertMessage("\(item.value) clicked")
        })
        tableView.dataSource = dataSource
        tableView.delegate = delegate
    }

}

CollectionView

class CollectionViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var dataSource: OkCollectionViewDataSource<Item, CollectionViewCell>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dataSource = OkCollectionViewDataSource()
        collectionView.dataSource = dataSource
    }
    
}

请注意,OkCollectionViewDataSource 需要一个对象和一个 collectionViewCell(它必须符合 OkViewCell 协议)

class CollectionViewCell: UICollectionViewCell, OkViewCell {
    
    @IBOutlet weak var valueLabel: UILabel!
    
    func configureItem(item: Item) {
        valueLabel.text = item.value
    }
}

Storyboard 中,单元格需要有一个类似下面的标识符(例如:CollectionViewCell

CollectionViewCell - 1 CollectionViewCell - 2

如果您想接收关于项目选择的反馈,需要将一个 OkCollectionViewDelegate 添加到您的 ViewController 中,这样在用户点击项目时,就会调用 onItemClicked 块。

class CollectionViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var dataSource: OkCollectionViewDataSource<Item, CollectionViewCell>!
    var delegate: OkCollectionViewDelegate<OkCollectionViewDataSource<Item, CollectionViewCell>, CollectionViewController>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        dataSource = OkCollectionViewDataSource()
        delegate = OkCollectionViewDelegate(dataSource: dataSource,
            onItemClicked: { (item, position) in
                self.showAlertMessage("\(item.value) clicked")
        })
        collectionView.dataSource = dataSource
        collectionView.delegate = delegate
    }
       
}

##此外,他们的OkDelegates还支持PullToRefresh和Pagination功能

PullToRefresh

使用tableViews

delegate.setOnPullToRefresh(tableView) { (refreshControl) -> Void in
            print("refreshed")    
            // You need to stop the UIRefreshControl manually when you want to
            refreshControl.endRefreshing()
        }

使用collectionViews

delegate.setOnPullToRefresh(collectionView) { (refreshControl) -> Void in
            print("refreshed")   
             // You need to stop the UIRefreshControl manually when you want to
            refreshControl.endRefreshing()
        }

Pagination

适用于tableViews和collectionViews

delegate.setOnPagination { (item) -> Void in
           // Ask for more items
        }

##更多自定义

如果您需要完全自定义 DataSourceDelegate (TableViewCollectionView),您只需创建一个从它们继承的类,并仅覆盖您需要的那些方法

class ExampleTableViewDataSource: OkTableViewDataSource<Item, TableViewCell> {
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let item = itemAtIndexPath(indexPath)

        var cell: TableViewCell!

        // Some condition        
        if indexPath.row == 0 {
            cell = tableView.dequeueReusableCellWithIdentifier("Some Custom Identifier 1", forIndexPath: indexPath) as! TableViewCell
        } else {
            cell = tableView.dequeueReusableCellWithIdentifier("Some Custom Identifier 2", forIndexPath: indexPath) as! TableViewCell
        }
        cell.configureItem(item)
    return cell
}

##RxSwift

有两种委托 OkRxTableViewDelegate 和 OkRxCollectionViewDelegate,可以使用它们来替代常规委托

RxSwift为Pagination和PullToRequest功能提供了额外的功能

TableView

delegate = OkRxTableViewDelegate(dataSource: dataSource,
            onItemClicked: { (item, position) in
                self.showAlertMessage("\(item.value) clicked")
        })
        delegate.setOnPullToRefresh(tableView) {
            return Observable.just(self.getMockItems())
        }
        delegate.setOnPagination { (item) -> Observable<[Item]> in
            return Observable.just(self.getMockItems(self.dataSource.items.count))
        }

CollectionView

delegate = OkRxCollectionViewDelegate(dataSource: dataSource,
            onItemClicked: { (item, position) in
                self.showAlertMessage("\(item.value) clicked")
        })
        delegate.setOnPullToRefresh(collectionView) {
            return Observable.just(self.getMockItems())
        }
        delegate.setOnPagination { (item) -> Observable<[Item]> in
            return Observable.just(self.getMockItems(self.dataSource.items.count))
        }   

PagerView

以下是一个示例 PagerViewController

最方便的方法是使用Storyboard。将一个视图拖到Storyboard中,并将类设置为 OkPagerView

您的ViewController需要实现 OkPagerViewDataSource 协议

public protocol OkPagerViewDataSource {
    
    func viewControllerAtIndex(index: Int) -> UIViewController?
    func numberOfPages() -> Int?
}

如果您愿意,还有一个 OkPagerViewDelegate 协议

public protocol OkPagerViewDelegate {
    
    func onPageSelected(viewController: UIViewController, index: Int)
}

滑动选项卡

以下是一个示例 PagerViewController

最便捷的方法是使用Storyboard。将视图拖到Storyboard上,将类设置为OkSlidingTabs

您可以通过Storyboard自定义这些属性

  • xPadding: CGFloat
  • xMargin: CGFloat
  • labelTextColor: UIColor
  • labelBgColor: UIColor
  • indicatorColor: UIColor
  • indicatorHeight: CGFloat
  • distributeEvenly: Bool

如果您想使用不同的字体,您需要执行:slidingTabs.font = newFont

您的ViewController需要实现OkSlidingTabsDataSource协议

public protocol OkSlidingTabsDataSource {
    func numberOfTabs() -> Int
    func titleAtIndex(index: Int) -> String
}

如果需要,还有OkSlidingTabsDelegate协议

public protocol OkSlidingTabsDelegate {
    func onTabSelected(index: Int)
}

##致谢此方法基于https://github.com/Karumi/BothamUI