美文网首页
iOS开发TableView封装

iOS开发TableView封装

作者: 门前一条小河流 | 来源:发表于2018-05-15 13:52 被阅读0次

直接上干货

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) ;

}

相关文章

网友评论

      本文标题:iOS开发TableView封装

      本文链接:https://www.haomeiwen.com/subject/qhwedftx.html