zoukankan      html  css  js  c++  java
  • UICollectionView

    -(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

  • 相关阅读:
    2013年10月17日 搬出来了
    如何与领导相处
    WEB系统开发
    C++ 常用术语(后续补充)
    C++ 构造函数放置默认转换explicit关键字(2)
    工作与生活
    C++类型转化分析(1)
    (一)win7下cocos2d-x 21 + vs2010
    为了生活
    iOS
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4975188.html
Copyright © 2011-2022 走看看