围绕 UITableView 和 UICollectionView 提供的包装,使使用更简单。它还提供了一个自定义的分组表格视图,灵感来自于 Twitter Bootstrap。DCCollectionViewLayout 提供了动态/可变大小单元的支持。OS X 版本可以在 这里 找到。
需要 Quartz 框架。
//first create a tableSource datasource manager
DCTableSource *tableSource = [[DCTableSource alloc] init];
tableSource.delegate = self;
//next create a tableview and assign it's delegate and datasource to the manager
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped]; // UITableViewStyleGrouped
self.tableView.delegate = tableSource;
self.tableView.dataSource = tableSource;
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithRed:245/255.0f green:245/255.0f blue:245/255.0f alpha:1];//[UIColor whiteColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
yourObject* object = [[yourObject alloc] init]; //your custom objects, can be any kind of object
object.text = @"Hello World!";
[tableSource.items addObject:object];
//then implement this delegate to map the object to cell associates.
-(Class)classForObject:(id)object
{
if([object isKindOfClass:[yourobject class]])
return [MessageCell class];
return nil;
}
//then in your MessageCell.m class (which is a subclass of DCTableViewCell provided):
+(CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)object
{
return 44; //the normal UITableViewCell
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
//the usual UITableViewCell init method
self.textLabel.textColor = [UIColor greenColor]; //just an example
return self;
}
}
-(void)layoutSubviews
{
[super layoutSubviews];
//layout your subviews as you normally would
}
-(void)setObject:(id)object
{
[super setObject:object];
//example of using your custom object to set the textLabel of UITableViewCell
yourObject* item = object;
self.textLabel.text = item.text; //this would be "Hello World!" as the example above
//the rest of your your custom logic here
}
DCCollectionSource *dataSource = [[DCCollectionSource alloc] init];
dataSource.delegate = self;
DCCollectionViewLayout *layout = [[DCCollectionViewLayout alloc] initWithDataSource:dataSource];
layout.enabledSpring = YES;
layout.horizontalSpacing = 0;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
self.collectionView.delegate = self.dataSource;
self.collectionView.dataSource = self.dataSource;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
yourObject* object = [[yourObject alloc] init]; //your custom objects, can be any kind of object
object.text = @"Hello World!";
[dataSource.items addObject:object];
//then implement this delegate to map the object to cell associates.
-(Class)classForObject:(id)object
{
if([object isKindOfClass:[yourobject class]])
return [MessageCell class];
return nil;
}
//then in your MessageCell.m class (which is a subclass of DCCollectionViewCell provided):
+(CGSize)collectionView:(UICollectionView*)collectionView sizeForObject:(id)object
{
return CGSizeMake(collectionView.frame.size.width, 88);//CGSizeMake(56.7, 56.7);
}
-(void)layoutSubviews
{
[super layoutSubviews];
//layout your subviews as you normally would
}
-(void)setObject:(id)object
{
[super setObject:object];
yourObject* item = object;
//the rest of your your custom logic here
}
此框架需要至少 iOS 5 及以上版本。
推荐使用 CocoaPods 软件包管理器安装 DCDataViews,因为它提供了灵活的依赖项管理和简单的安装过程。
通过 CocoaPods
如果尚未安装,请安装 CocoaPods。
$ [sudo] gem install cocoapods
$ pod setup
更改到 Xcode 项目的目录,创建并编辑您的 Podfile 并添加 DCDataViews
$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
platform :ios, '5.0'
pod 'DCDataViews'
将 DCDataViews 安装到您的项目中
$ pod install
从 .xcworkspace 文件(而不是常规的项目文件)在 Xcode 中打开您的项目
DCDataViews 是 Apache 许可下的软件。