c_cpp 多个部分的通用UITableView / UICollectionView数据源

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 多个部分的通用UITableView / UICollectionView数据源相关的知识,希望对你有一定的参考价值。

//
//  Copyright © 2013 Yuri Kotov
//

#import <Foundation/Foundation.h>

@protocol ADVSectionInfo <NSObject>

    @required
@property (readonly, nonatomic) NSArray *objects;
@property (readonly, nonatomic) NSUInteger numberOfObjects;

    @optional
@property (readonly, nonatomic) NSString *name;
@property (readonly, nonatomic) NSString *indexTitle;

@end
//
//  Copyright © 2013 Yuri Kotov
//

#import "ADVDataSource.h"
#import "ADVSectionInfo.h"

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

@implementation ADVDataSource

#pragma mark - ADVManagedDataSource
- (instancetype) initWithSections:(NSArray *)sections
           reusableCellIdentifier:(NSString *)identifier
           cellConfigurationBlock:(ADVCellConfigurationBlock)block
{
    if ((self = [super init]))
    {
        _sections = sections;
        _configurationBlock = block;
        _cellIdentifier = identifier;
    }
    return self;
}

- (NSInteger) numberOfSections
{
    return self.sections.count;
}

- (NSInteger) numberOfItemsInSection:(NSInteger)section
{
    id <ADVSectionInfo> sectionInfo = self.sections[section];
    return sectionInfo.numberOfObjects;
}

- (id) view:(id)view cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    id <ADVSectionInfo> sectionInfo = self.sections[indexPath.section];
    id object = sectionInfo.objects[indexPath.row];
    UITableViewCell *cell = [view dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    self.configurationBlock(cell, object);
    return cell;
}

#pragma mark - UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self numberOfSections];
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <ADVSectionInfo> sectionInfo = self.sections[section];
    return sectionInfo.name;
}

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

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

#pragma mark - UICollectionViewDataSource
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.numberOfSections;
}

- (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
//
//  Copyright © 2013 Yuri Kotov
//

#import <Foundation/Foundation.h>

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

@interface ADVDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>

@property (readonly, nonatomic) NSArray *sections;

- (instancetype) initWithSections:(NSArray *)sections
           reusableCellIdentifier:(NSString *)identifier
           cellConfigurationBlock:(ADVCellConfigurationBlock)block;

@end

以上是关于c_cpp 多个部分的通用UITableView / UICollectionView数据源的主要内容,如果未能解决你的问题,请参考以下文章

无论如何创建一个带有一个通用页脚的 UITableView 吗?

UICollectionView/UITableView 通用应用

UITableView 由两个 NSFetchedResultsController 驱动,结果通用

UITableView 具有多个具有多行的部分。我需要部分独立运行

UITableView 和 UIButtons 中的多个部分 swift

uitableview 中的多个原型单元,带有部分