ALDataSource 0.3.1

ALDataSource 0.3.1

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

Aziz U. Latypov 维护。



  • 作者:
  • Aziz U. Latypov

使用 NSFetchedResultsController 的 DataSource

将以下代码导入您的 pch 文件

#import <ALDataSource/ALDataSource.h>

DataSource 的子类

ALEntityTableViewDataSource.h

#import "ALTableViewDataSourceWithFetchedResultsController.h"

@interface ALEntityTableViewDataSource : ALTableViewDataSourceWithFetchedResultsController

@end

ALEntityTableViewDataSource.m

#import "ALEntityTableViewDataSource.h"
#import <ALCoreDataManager/ALCoreDataManager+Singleton.h>

@implementation ALEntityTableViewDataSource

- (instancetype)initWithCellConfigurationBlock:(ALTableViewCellConfigurationBlock)cellConfigurationBlock
                        andReuseIdentiferBlock:(ALTableViewCellReuseIdentiferBlock)reuseIdentifierBlock
{
    NSManagedObjectContext *managedObjectContext = 
    [ALCoreDataManager defaultManager].managedObjectContext;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity"
                                              inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];

    self = [super initWithFetchRequest:fetchRequest
                  managedObjectContext:managedObjectContext
                cellConfigurationBlock:cellConfigurationBlock
                andReuseIdentiferBlock:reuseIdentifierBlock];
    if (self) {

    }
    return self;
}

@end

在 Storyboard 中添加一个 UITableViewController 并为其创建子类

@interface ALEntityTableViewController () <UIAlertViewDelegate>

@property (nonatomic, strong) ALEntityTableViewDataSource *dataSource;

@end

@implementation ALEntityTableViewController

- (ALEntityTableViewDataSource*)dataSource
{
    if (!_dataSource) {

        __weak typeof(self) weakSelf = self;
        ALTableViewCellConfigurationBlock cellConfigurationBlock = ^(UITableViewCell *cell, NSIndexPath *indexPath){
            Entity *entity = (Entity*)[weakSelf.dataSource itemAtIndexPath:indexPath];
            cell.textLabel.text = entity.title;
        };

        ALTableViewCellReuseIdentiferBlock reuseIdentifierBloc = ^(NSIndexPath *indexPath){
            return @"Cell";
        };

        _dataSource =
        [[ALEntityTableViewDataSource alloc] initWithCellConfigurationBlock:cellConfigurationBlock
                                                     andReuseIdentiferBlock:reuseIdentifierBloc];
    }
    return _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataSource.tableView = self.tableView; //also sets the tableView's dataSource
}

@end

您还可以如下设置 dataSource 的 predicate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length]) {
        self.dataSource.predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@",searchText];
    }else{
        self.dataSource.predicate = nil;
    }
}

使用 MutableArray 的 DataSource

ALEntityTableViewDataSource.h

#import <ALDataSource/ALCollectionViewDataSourceWithMutableArray.h>

@interface ALEntityCollectionViewDataSource : ALCollectionViewDataSourceWithMutableArray

- (instancetype)initWithCellConfigurationBlock:(ALCollectionViewCellConfigurationBlock)cellConfigurationBlock
                        andReuseIdentiferBlock:(ALCollectionViewCellReuseIdentiferBlock)reuseIdentifierBlock;


@end

ALEntityTableViewDataSource.m

#import "ALEntityCollectionViewDataSource.h"

@implementation ALEntityCollectionViewDataSource

- (instancetype)initWithCellConfigurationBlock:(ALCollectionViewCellConfigurationBlock)cellConfigurationBlock
                        andReuseIdentiferBlock:(ALCollectionViewCellReuseIdentiferBlock)reuseIdentifierBlock
{
    NSArray *items = @[@"item1",@"item2",@"item3"];
    self = [super initWithArray:items
         cellConfigurationBlock:cellConfigurationBlock
         andReuseIdentiferBlock:reuseIdentifierBlock];
    if (self) {

    }
    return self;
}

@end

在 Storyboard 中添加一个 UICollectionViewController 并为其创建子类

@interface ALEntityCollectionViewController ()

@property (nonatomic, strong) ALEntityCollectionViewDataSource *dataSource;

@end

@implementation ALEntityCollectionViewController

#pragma mark - Data Source -

- (ALEntityCollectionViewDataSource*)dataSource
{
    if (!_dataSource) {

        __weak typeof(self) weakSelf = self;
        ALCollectionViewCellConfigurationBlock cellConfigurationBlock = ^(UICollectionViewCell *cell, NSIndexPath *indexPath){
            NSString *text = [weakSelf.dataSource itemAtIndexPath:indexPath];
            ALEntityCollectionViewCell *entityCell = (ALEntityCollectionViewCell*)cell;
            entityCell.myTextLabel.text = text;
        };

        ALCollectionViewCellReuseIdentiferBlock reuseIdentifierBloc = ^(NSIndexPath *indexPath){
            return @"Cell";
        };

        _dataSource =
        [[ALEntityCollectionViewDataSource alloc] initWithCellConfigurationBlock:cellConfigurationBlock
                                                          andReuseIdentiferBlock:reuseIdentifierBloc];
    }
    return _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataSource.collectionView = self.collectionView;
}

您还可以如下设置 dataSource 的 predicate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length]) {
        self.dataSource.predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@",searchText];
    }else{
        self.dataSource.predicate = nil;
    }
}

现在您可以如下添加任何类型的对象到此 dataSource

[(ALEntityCollectionViewDataSource*)self.dataSource addObject:text];

设置 predicate 以进行过滤

if ([searchText length]) {
    self.dataSource.predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[c] %@",searchText];
}else{
    self.dataSource.predicate = nil;
}

需求

安装

作者

Aziz U. Latypov,[email protected]

许可证

ALDataSource 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。