代码改变世界

UICollectionView

2015-11-18 17:15  xiangjune  阅读(232)  评论(0编辑  收藏  举报

-(void)drawContentGridView:(int) yHeight

{

    // 设置layout

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    layout.itemSize = CGSizeMake(230, 160);

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, yHeight + 20, self.frame.size.width, self.frame.size.height - yHeight - 20) collectionViewLayout:layout];

    

    // 设置代理

    self.collectionView.dataSource = self;

    self.collectionView.delegate = self;

    

    self.collectionView.backgroundColor = [UIColor clearColor];

    

    // 注册cellView

    [self.collectionView registerClass:[DetailAnswerResultCollectionViewCell class] forCellWithReuseIdentifier:@"DetailAnswerResultCollectionViewCellFlag"];

    

    [self addSubview:self.collectionView];

}

 

 

// 实现代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return answerDataArray.count;

}

 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

 

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *identifyStringFlag = @"DetailAnswerResultCollectionViewCellFlag";

    DetailAnswerResultCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifyStringFlag forIndexPath:indexPath];

    

    int position = indexPath.row;

    if(position >= 0 && position < answerDataArray.count)

    {

        cell.textTitleLable.text = [answerDataArray objectAtIndex:position];

        

        if(position%2==0)

        {

            cell.textAnswerLable.text = @"答案:A";

            cell.textAnswerLable.hidden = NO;

            cell.imageAnswerShotImageView.hidden = YES;

            cell.resultImageView.hidden = NO;

            cell.resultImageView.image = [UIImage imageNamed:@"judge_correct.png"];

        }

        else

        {

            cell.textAnswerLable.hidden = YES;

            cell.imageAnswerShotImageView.hidden = NO;

            cell.imageAnswerShotImageView.image = [UIImage imageNamed:@"paper_list_homework_item_icon"];

            cell.resultImageView.hidden = NO;

            cell.resultImageView.image = [UIImage imageNamed:@"judge_correct.png"];

        }

    }

    

    return cell;

}

 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(230, 160);

}

 

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(15, 15, 15, 15);

}

 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    int position = indexPath.row;

    if(position >= 0 && position < answerDataArray.count)

    {

        NSLog(@"CLICK ITEM positoin=%i, content=%@", position, [answerDataArray objectAtIndex:position]);

    }

}

 

 

 // 自定义cell

@implementation DetailAnswerResultCollectionViewCell

 

CGRect cellFrame;

 

-(id)initWithFrame:(CGRect)frame

{

    cellFrame = frame;

    if(self = [super initWithFrame:cellFrame])

    {

        [self drawCellView];

    }

    return self;

}

 

-(void)drawCellView

{

    _textTitleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cellFrame.size.width, 30)];

    _textTitleLable.font = [UIFont systemFontOfSize:18];

    _textTitleLable.textColor = [UIColor whiteColor];

    _textTitleLable.backgroundColor = CellColor_LightBlue;

    

    _textAnswerLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, cellFrame.size.width, 50)];

    _textAnswerLable.font = [UIFont systemFontOfSize:20];

    _textAnswerLable.textColor = [UIColor blackColor];

    _textAnswerLable.textAlignment = NSTextAlignmentCenter;

    

    _imageAnswerShotImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 50, cellFrame.size.width, 80)];

    _imageAnswerShotImageView.contentMode = UIViewContentModeScaleAspectFit;

    _imageAnswerShotImageView.hidden = YES;

    

    

    _resultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(cellFrame.size.width - 50, cellFrame.size.height - 50, 30, 30)];

    _resultImageView.hidden = YES;

    

    self.backgroundColor = CellColor_LightGray;

    [self addSubview:_textTitleLable];

    [self addSubview:_textAnswerLable];

    [self addSubview:_imageAnswerShotImageView];

    [self addSubview:_resultImageView];

}

 

@end