JVRBaseTableViewDataSource 0.0.7

JVRBaseTableViewDataSource 0.0.7

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2014年12月

József Vesza 维护。



一个基本的、可重用和可扩展的 UITableViewDataSource 类,用于简化您的 UITableView 类。要使用它,您还需要一个遵循 JVRCellConfiguratorDelegate 的类,该类将为表格视图设置单个单元格。有关使用方法的详细信息,请参见下面的简单示例或参考示例项目。

配置表格视图单元格

单元格配置器类旨在封装之前在 UITableViewDataSource 协议中找到的 tableView:cellForRowAtIndexPath: 方法中的逻辑。它们可以用来根据要显示的对象返回单元格的重用标识符,以及自定义其外观或行为。

// MyCellConfigurator.h
#import "JVRBaseTableViewDataSource.h"

@interface MyCellConfigurator : NSObject<JVRCellConfiguratorDelegate>
@end

// MyCellConfigurator.m
@implementation MyCellConfigurator

- (NSString *)fetchCellIdentifierForObject:(id)anObject
{
    if ([anObject isKindOfClass:[MyClass class]])
    {
        return @"myCell";
    }

    return @"regularCell";
}

- (UITableViewCell *)configureCell:(MyCell *)cell usingObject:(MyClass *)object
{
    cell.titleLabel.text = object.stringProperty;
    return cell;
}

@end

表格视图控制器中的示例用法

通过使用 JVRBaseTableViewDataSource,您可以从 UITableViewController 类中省略许多样板代码。通过继承或编辑此类并实现 numberOfSectionsInTableView: 方法,可以支持表格视图的分类。

// MyTableViewController.m
#import "JVRBaseTableViewDataSource.h"
#import "MyCellConfigurator.h"

@interface MyTableViewController ()

@property (nonatomic, strong) JVRBaseTableViewDataSource *dataSource;

@end

@implementation MyTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupDataSource];
}

- (void)setupDataSource
{
    self.dataSource = [JVRBaseTableViewDatasource datasourceForTableView:self.tableView
                                                               withItems:@[@"test", @"item"]
                                                   usingCellConfigurator:[[MyCellConfigurator alloc] init]];
}

@end

安装

您可以直接将源文件复制到您的项目中,但推荐的方式是通过带有以下 Podfile 的 CocoaPods 在您的项目中使用此类

platform :ios, "7.1"

pod 'JVRBaseTableViewDataSource'