- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
BDStoryShelfCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[BDStoryShelfCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
}
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger startIndex = indexPath.row * KeyStoryShelfBookCount;
NSInteger step = KeyStoryShelfBookCount;
for(int i = 0;i < step;i++) {
NSInteger index = startIndex + i;
if(index < self.bookListArray.count) {
BDStoryBookDetailInfo *bookInfo = [self.bookListArray objectAtIndex:index];
[array addObject:bookInfo];
}
}
[cell refreshWithData:array];
return cell;
}
//**********************************************************************************
#import "BDStoryBaseTableViewCell.h"
#import "BDStroyBookListModel.h"
#import "BDStoryBookDetailInfo.h"
#define KeyStoryShelfCellHeight 118
#define KeyStoryShelfBookCount (BDIPHONE_SCREEN_WIDTH_PORT>320?4:3) //书架中每行所放书的个数
@protocol BDStoryShelfCellDelegate <NSObject>
- (void)addBookButtonClicked;
@end
/**
* 书架 cell
*/
@interface BDStoryShelfCell :BDStoryBaseTableViewCell
@property (nonatomic,assign) id<BDStoryShelfCellDelegate> delegate;
- (void)refreshWithData:(NSArray *)array;
@end
//--------------------------------------------------------------------------------------------------------------------------------------
#import "BDStoryShelfCell.h"
#import "BDStoryRelateSubCell.h"
#import "BDStoryShelfSubCell.h"
#import "BDStoryCommonModel.h"
#define KeySubCellOffY 12
#define KeyAddButtonWidth 62
#define KeyAddButtonHeight 86
#define KeySubCellLeft 20//图标距离左间距距离
#define KeySubCellGap ((BDIPHONE_SCREEN_WIDTH_PORT-KeySubCellLeft*2-BDStoryShelfCellWidth*KeyStoryShelfBookCount)/(CGFloat)(KeyStoryShelfBookCount-1))//两图标之间的间距
@interface BDStoryShelfCell()
@property (nonatomic,strong) UIImageView *bottomImageView;
@property (nonatomic,strong) UIButton *addButton;//添加书架图标
@property (nonatomic,strong) NSMutableArray *subCells;
@end
@implementation BDStoryShelfCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
if (!self.bottomImageView) {
self.bottomImageView = [[UIImageViewalloc] init];
}
self.bottomImageView.backgroundColor = [UIColorclearColor];
self.bottomImageView.frame =CGRectMake(0,KeyStoryShelfCellHeight-21,BDIPHONE_SCREEN_WIDTH_PORT, 21);
self.bottomImageView.image = [UIImageimageNamed:@"bookshelf"];
[selfaddSubview:self.bottomImageView];
if (!self.addButton) {
self.addButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
}
self.addButton.backgroundColor = [UIColorclearColor];
[self.addButtonsetBackgroundImage:[UIImageimageNamed:@"add"]forState:UIControlStateNormal];
[self.addButtonaddTarget:selfaction:@selector(addButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
self.addButton.hidden =YES;
[selfaddSubview:self.addButton];
self.subCells = [[NSMutableArrayalloc] init];
for (int i =0; i < KeyStoryShelfBookCount; i++) {
BDStoryShelfSubCell *subCell = [[BDStoryShelfSubCellalloc] init];
subCell.backgroundColor = [UIColorredColor];
subCell.frame =CGRectMake(KeySubCellLeft + (KeySubCellGap+BDStoryShelfCellWidth)*i,KeySubCellOffY, BDStoryShelfCellWidth,BDStoryShelfCellHeight);
subCell.hidden =YES;
[selfaddSubview:subCell];
[self.subCellsaddObject:subCell];
}
}
returnself;
}
/**
* 刷新数据
*/
- (void)refreshWithData:(NSArray *)array
{
for (int i =0; i < array.count; i++) {
BDStoryShelfSubCell *subCell = [self.subCellsobjectAtIndex:i];
subCell.hidden =NO;
[subCell refreshWithData:[array objectAtIndex:i]];
}
if (array.count <self.subCells.count) {
for (int i = (int)array.count; i < (int)self.subCells.count ; i++) {
BDStoryShelfSubCell *subCell = [self.subCellsobjectAtIndex:i];
subCell.hidden =YES;
}
}
if (array.count <KeyStoryShelfBookCount) {
self.addButton.hidden =NO;
self.addButton.frame =CGRectMake(KeySubCellLeft+(KeySubCellGap+BDStoryShelfCellWidth)*(array.count%KeyStoryShelfBookCount),KeySubCellOffY-KeyCoverImageOffSet,KeyAddButtonWidth, KeyAddButtonHeight);
} else {
self.addButton.hidden =YES;
}
return;
}
#pragma mark - Methods
- (void)addButtonClicked:(UIButton *)sender
{
if (self.delegate && [self.delegaterespondsToSelector:@selector(addBookButtonClicked)]) {
[self.delegateaddBookButtonClicked];
}
}
@end