直接上干货
typedefvoid (^TableViewCellConfigureBlock)(NSIndexPath *indexPath,iditem, UITableViewCell *cell) ;
typedefCGFloat (^CellHeightBlock)(NSIndexPath *indexPath,iditem) ;
typedefvoid (^DidSelectCellBlock)(NSIndexPath *indexPath,iditem) ;
@interfaceTLMusicDataDelegate : NSObject
@property(nonatomic,strong) NSMutableArray *sourceItem ;
- (instancetype)initWithItem:(NSMutableArray *)sourceItem
cellIdentifier:(NSString *)cellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)cellBlock
cellHightBlock:(CellHeightBlock)heightBlock
didSelectCellBlock:(DidSelectCellBlock)selectBlock;
- (void)handleTableViewDatasourceAndDelegate:(UITableView *)table ;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath ;
#import "TLMusicDataDelegate.h"
@interface TLMusicDataDelegate()
@property(nonatomic,copy)NSString*cellIdentifier ;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock ;
@property(nonatomic,copy)CellHeightBlockcellHeightBlock;
@property (nonatomic, copy) DidSelectCellBlock didSelectBlock;
@property (nonatomic, weak) UITableView *tableView;
@end
@implementationTLMusicDataDelegate
- (instancetype)initWithItem:(NSMutableArray*)sourceItem
cellIdentifier:(NSString*)cellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)cellBlock
cellHightBlock:(CellHeightBlock)heightBlock
didSelectCellBlock:(DidSelectCellBlock)selectBlock
{
self= [superinit];
if(self){
self.sourceItem= sourceItem;
self.cellIdentifier= cellIdentifier;
self.configureCellBlock= cellBlock;
self.cellHeightBlock= heightBlock;
self.didSelectBlock= selectBlock;
}
return self;
}
- (id)itemAtIndexPath:(NSIndexPath*)indexPath
{
returnself.sourceItem[(int)indexPath.row] ;
}
- (void)handleTableViewDatasourceAndDelegate:(UITableView*)table
{
table.dataSource=self;
table.delegate =self;
}
- (void)setSourceItem:(NSMutableArray*)sourceItem
{
_sourceItem= sourceItem;
[self.tableView reloadData];
}
#pragma mark --
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return self.sourceItem.count ;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier] ;
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
iditem = [selfitemAtIndexPath:indexPath] ;
self.configureCellBlock(indexPath,item,cell) ;
returncell ;
}
#pragma mark --
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
iditem = [selfitemAtIndexPath:indexPath] ;
returnself.cellHeightBlock(indexPath,item) ;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
iditem = [selfitemAtIndexPath:indexPath] ;
self.didSelectBlock(indexPath,item) ;
}
网友评论