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

  • 相关阅读:
    JS: 子项可以来回交换的两个下拉列表
    DOM事件
    DOM基础2——元素
    DOM基础1
    JS: 随机点名程序与万年历
    G_S男女匹配算法(算法的第一个程序2016.09.19)
    Java IO流详尽解析(大神之作)
    细讲解JAVA中的IO流
    c++运算符的优先级(收好不谢)
    java程序——输出当月日历表
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4975188.html
Copyright © 2011-2022 走看看