Skip to content

Instantly share code, notes, and snippets.

@advantis
Last active December 20, 2015 10:59

Revisions

  1. advantis revised this gist Oct 15, 2013. 3 changed files with 19 additions and 5 deletions.
    3 changes: 2 additions & 1 deletion ADVArrayDataSource.h
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,11 @@
    //

    #import <Foundation/Foundation.h>
    #import "ADVIndexedSubscripting.h"

    typedef void(^ADVCellConfigurationBlock)(id cell, id object);

    @interface ADVArrayDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>
    @interface ADVArrayDataSource : NSObject <ADVIndexedSubscripting, UITableViewDataSource, UICollectionViewDataSource>

    @property (readonly, nonatomic) NSArray *array;

    12 changes: 8 additions & 4 deletions ADVArrayDataSource.m
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,12 @@ - (void) deleteObjectAtIndexPath:(NSIndexPath *)indexPath
    [_array removeObjectAtIndex:indexPath.row];
    }

    #pragma mark - ADVIndexedSubscripting
    - (id) objectAtIndexedSubscript:(NSUInteger)index
    {
    return self.array[index];
    }

    #pragma mark - UITableViewDataSource
    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    @@ -46,9 +52,8 @@ - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    id object = self.array[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    self.configurationBlock(cell, self[indexPath.row]);
    return cell;
    }

    @@ -75,10 +80,9 @@ - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInS
    - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
    cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    id object = self.array[indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier
    forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    self.configurationBlock(cell, self[indexPath.row]);
    return cell;
    }

    9 changes: 9 additions & 0 deletions ADVIndexedSubscripting.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    //
    // Copyright © 2013 Yuri Kotov
    //

    #import <Foundation/Foundation.h>

    @protocol ADVIndexedSubscripting <NSObject>
    - (id) objectAtIndexedSubscript:(NSUInteger)index;
    @end
  2. advantis revised this gist Sep 27, 2013. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions ADVArrayDataSource.m
    Original file line number Diff line number Diff line change
    @@ -47,9 +47,9 @@ - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    id object = self.array[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    }

    - (void) tableView:(UITableView *)tableView
    @@ -76,10 +76,10 @@ - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
    cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    id object = self.array[indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier
    forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier
    forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    }

    @end
  3. advantis revised this gist Sep 27, 2013. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions ADVArrayDataSource.m
    Original file line number Diff line number Diff line change
    @@ -33,14 +33,6 @@ - (NSInteger) numberOfItemsInSection:(NSInteger)section
    return self.array.count;
    }

    - (id) view:(id)view cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    id object = self.array[indexPath.row];
    UITableViewCell *cell = [view dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    }

    - (void) deleteObjectAtIndexPath:(NSIndexPath *)indexPath
    {
    [_array removeObjectAtIndex:indexPath.row];
    @@ -54,7 +46,10 @@ - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return [self view:tableView cellForItemAtIndexPath:indexPath];
    id object = self.array[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    }

    - (void) tableView:(UITableView *)tableView
    @@ -80,7 +75,11 @@ - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInS
    - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
    cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    return [self view:collectionView cellForItemAtIndexPath:indexPath];
    id object = self.array[indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier
    forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    }

    @end
  4. Yuri Kotov created this gist Jul 31, 2013.
    17 changes: 17 additions & 0 deletions ADVArrayDataSource.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    //
    // Copyright © 2013 Yuri Kotov
    //

    #import <Foundation/Foundation.h>

    typedef void(^ADVCellConfigurationBlock)(id cell, id object);

    @interface ADVArrayDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>

    @property (readonly, nonatomic) NSArray *array;

    - (instancetype) initWithArray:(NSArray *)array
    reusableCellIdentifier:(NSString *)identifier
    cellConfigurationBlock:(ADVCellConfigurationBlock)block;

    @end
    86 changes: 86 additions & 0 deletions ADVArrayDataSource.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    //
    // Copyright © 2013 Yuri Kotov
    //

    #import "ADVArrayDataSource.h"

    @interface ADVArrayDataSource ()
    @property (readonly, nonatomic) NSString *cellIdentifier;
    @property (readonly, nonatomic) ADVCellConfigurationBlock configurationBlock;
    @end

    @implementation ADVArrayDataSource
    {
    NSMutableArray *_array;
    }

    #pragma mark - ADVArrayDataSource
    - (instancetype) initWithArray:(NSArray *)array
    reusableCellIdentifier:(NSString *)identifier
    cellConfigurationBlock:(ADVCellConfigurationBlock)block
    {
    if ((self = [super init]))
    {
    _configurationBlock = block;
    _cellIdentifier = identifier;
    _array = [array mutableCopy];
    }
    return self;
    }

    - (NSInteger) numberOfItemsInSection:(NSInteger)section
    {
    return self.array.count;
    }

    - (id) view:(id)view cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    id object = self.array[indexPath.row];
    UITableViewCell *cell = [view dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
    }

    - (void) deleteObjectAtIndexPath:(NSIndexPath *)indexPath
    {
    [_array removeObjectAtIndex:indexPath.row];
    }

    #pragma mark - UITableViewDataSource
    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [self numberOfItemsInSection:section];
    }

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return [self view:tableView cellForItemAtIndexPath:indexPath];
    }

    - (void) tableView:(UITableView *)tableView
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    switch (editingStyle)
    {
    case UITableViewCellEditingStyleDelete:
    [self deleteObjectAtIndexPath:indexPath];
    break;
    default:
    break;
    }
    }

    #pragma mark - UICollectionViewDataSource
    - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
    return [self numberOfItemsInSection:section];
    }

    - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
    cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    return [self view:collectionView cellForItemAtIndexPath:indexPath];
    }

    @end