OFAPopulator 0.1.1

OFAPopulator 0.1.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2015年6月

Manuel MeyerManuel Meyer 维护。



一次解决所有TableView和CollectionView填充问题

通过使用适配器模式,OFACore的目标是让填充TableView和CollectionView变得简单,并且便于子类化和重用组件。不会伤害MVC或限制delegate/datasource模式

填充包含一个部分的TableView

OFASectionPopulator *section1Populator = [[OFASectionPopulator alloc] initWithParentView:self.tableView
                                                                             dataFetcher:[[ExampleDataFetcher alloc] init]
                                                                               cellClass:[UITableViewCell class]
                                                                          cellIdentifier:^NSString* (id obj, NSIndexPath *indexPath){ return  indexPath.row % 2  ? @"Section1_1" : @"Section1_2" ; }
                                                                        cellConfigurator:^(id obj, UIView *view, NSIndexPath *indexPath)
{
    UITableViewCell *cell = (UITableViewCell *)view;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", obj];
    cell.textLabel.backgroundColor = [UIColor clearColor];
}];
self.populator = [[OFASectionedPopulator alloc] initWithParentView:self.tableView
                                                 sectionPopulators:@[section1Populator]];

填充包含相同数据的单个部分的CollectionView

OFASectionPopulator *section1Populator = [[OFASectionPopulator alloc] initWithParentView:self.collectionView
                                                                             dataFetcher:[[ExampleDataFetcher alloc] init]
                                                                               cellClass:[ExampleCollectionViewCell class]
                                                                          cellIdentifier:^NSString* (id obj, NSIndexPath *indexPath){ return  indexPath.row % 2  ? @"cell" : @"cell2" ; }
                                                                        cellConfigurator:^(id obj, UIView *view, NSIndexPath *indexPath)
{
    ExampleCollectionViewCell *cell = (ExampleCollectionViewCell *)view;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", obj];
}];


self.populator = [[OFASectionedPopulator alloc] initWithSectionPopulators:@[section1Populator]];

除了传递一个CollectionView和一个UICollectionViewCell类,代码是相同的。

tableview collectionview

Swift 示例

self.activityDataProvider = ActivityDataProvider(activities:activities)
let cellIdentifier = { (activity: AnyObject!, indexPath:NSIndexPath!) -> String! in
    let s : String! = "ActivityCell"
    return s
}

let cellConfigurator = { (obj :AnyObject!, cell: AnyObject!, indexPath:NSIndexPath!) -> Void in
    if let theCell = cell as? ActivityTableCell, activity = obj as? Activity,c = self.activityCellConfigurator{
        c(obj: activity, cell: theCell, indexPath: indexPath)
    }
}

let activitySectionPopulator = OFASectionPopulator(
    parentView: self.tableView,
    dataProvider: activityDataProvider as! OFADataProvider!,
    cellIdentifier: cellIdentifier,
    cellConfigurator:cellConfigurator
)

activitySectionPopulator.objectOnCellSelected = {[unowned self](obj :AnyObject!, cell: UIView!, indexPath:NSIndexPath!) -> Void in
    if let theCell = cell as? ActivityTableCell, activity = obj as? Activity, c = self.activitySelectedOnCell{
        c(obj: activity, cell: theCell, indexPath: indexPath)
    }
    if let activityCell = cell as? UITableViewCell{
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}

activitySectionPopulator.heightForCellAtIndexPath = {[unowned self](obj: AnyObject!, indexPath:NSIndexPath!) -> CGFloat in
    return ActivityTableCell.heightForActivity(self.activities[indexPath.row], width: self.tableView.frame.size.width)
}


self.activityTableViewPopulator = OFAViewPopulator(sectionPopulators: [activitySectionPopulator])
self.activityTableViewPopulator!.didScroll = {(scrollView:UIScrollView!) -> Void in
    let endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height
    if (endScrolling >= scrollView.contentSize.height - 400) {
        self.loadMore()
    }
}

安装

  • 通过 Cocoapods

    pod 'OFAPopulator'