ALDataSource 0.3.1

ALDataSource 0.3.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Mar 2015

Maintained by Aziz U. Latypov.



  • By
  • Aziz U. Latypov

DataSource with NSFetchedResultsController

Import to your pch file

#import <ALDataSource/ALDataSource.h>

Subclass a 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

Add a UITableViewController in storyboard and create subclass for it

@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

You can also set predicate on dataSource like this:

- (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 with MutableArray

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

Add a UICollectionViewController in storyboard and create subclass for it

@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;
}

You can also set predicate on dataSource like this:

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

Now you can add objects of any type to this dataSource like this

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

Set a predicate to filter

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

Requirements

Installation

Author

Aziz U. Latypov, [email protected]

License

ALDataSource is available under the MIT license. See the LICENSE file for more info.