kvc和collectionView和反选cell

1.//key值没找到对应的属性,会触发此方法

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{

//因为这里的id是系统自带的属性,所以不能在定义的model里,而是要设置成不是系统属性的ID,但是后面要用kvc语句所以model的.m文件先预判即可

    if ([key isEqualToString:@"id"]) {

        self.ID=value;

    }

//key 没有属性对应的key,可以打印查看

    NSLog(@"undefined key:%@",key);

}

****注意这里****

  [model setValuesForKeysWithDictionary:subDict];

 

                for (NSString *key in subDict) {

                    id obj = [subDict objectForKey:key];

                    [model setValue:obj forKey:key];

 

2.//从自定义xib中加载cell 会触发此方法,在.m文件中自己添加该方法

- (void)awakeFromNib{

    NSLog(@"awake!");

    //给cell设置选中之后的背景视图

     UIView *view = [[UIView alloc] init];

    view.backgroundColor = [UIColor lightGrayColor];

    //view的frame 会自动设置与cell bounds一致

    self.selectedBackgroundView = view;

    //self.backgroundView

}

3.图片的圆角设计

         iconImageView.layer.masksToBounds = YES;

         iconImageView.layer.cornerRadius = 8;

4.UICollectionView

#import "RootViewController.h"

#import "MyCell.h"

#import "CustomView.h"

@interface RootViewController ()

{

    //集合视图,iOS6之后出现的视图控件,与UITableView类似,最大的特点是可以对cell实现灵活的布局

    //UICollectionView 使用的cell为UICollectionViewCell

    UICollectionView *_collectionView;

}

@end

 

@implementation RootViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    //偏移量重置为0,0

    self.automaticallyAdjustsScrollViewInsets = NO;

    //UICollectionViewLayout 集合视图的布局类,是一个抽象基类,一般使用它的子类实现对cell的布局

    //UICollectionViewFlowLayout 系统提供的,能够实现cell的网格布局

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

    [flow setScrollDirection:UICollectionViewScrollDirectionVertical];

    //设置横向的最小间隔,如果CollectionView垂直滚动能直接生效

    flow.minimumLineSpacing = 3;

    //设置纵向的最小间隔,CollectionView垂直滚动 实际间隔需要根据item的size 和edgesInsets的值来调节

    flow.minimumInteritemSpacing =0;

    //利用布局对象,实现集合视图的初始化

    //UIScreen 代表手机屏幕类 ,单例

    //[UIScreen mainScreen].bounds.size 获取手机屏幕的大小

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) collectionViewLayout:flow];

    _collectionView.delegate = self;

    _collectionView.dataSource = self;

    

    //将xib定制的cell提前注册到UICollectionView

    //iOS6之后出现的技术,能够提高集合视图的运行效率

    [_collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    //[_collectionView registerClass:<#(__unsafe_unretained Class)#> forCellWithReuseIdentifier:<#(NSString *)#>]

    //注册CustomView为集合视图的sectionHeaderView,并设置重用标识符

    [_collectionView registerClass:[CustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"supple"];

    //设置SectionFooterView

    [_collectionView registerClass:[CustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"supple"];

    

    [self.view addSubview:_collectionView];

    

    // Do any additional setup after loading the view.

}

 

#pragma mark - Delegate

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

    NSLog(@"selected item:%d",indexPath.item);

}

 

#pragma mark - DataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

//一个section 有多少个Item(cell)

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

    return 10;

}

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

    static NSString *cellIde = @"cell";

    //通过重用标识符和indexPath到UICollectionView的重用队列中获取cell对象,因为已经提前注册了cell,如果获取不到cell对象,UICollectionView会根据注册的cell样式,自动创建cell对象

    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIde forIndexPath:indexPath];

    //为cell的控件赋值

    cell.headerImageView.image = [UIImage imageNamed:@"0.png"];

    cell.titleLabel.text = @"test";

    return cell;

}

 

//通过此方法为section的headerView和footerView赋值

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

   static NSString *viewIde = @"supple";

    //从重用队列中获取视图,取不到  则自动创建

    CustomView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:viewIde forIndexPath:indexPath];

    //可以为view赋值

    //判断是header还是footer 赋不同的值

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        view.backgroundColor = [UIColor yellowColor];

    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){

        view.backgroundColor = [UIColor blueColor];

    }

    return view;

}

 

 

#pragma mark - delegate flow layout

//指定每一个item的大小

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

    return CGSizeMake(150,120);

}

 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    //如果垂直滚动 view的width与集合视图一致,需要设置高度

    return CGSizeMake(30,30);

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

     return CGSizeMake(30,30);

}

 

//UIEdgeInsets 描述的是一个视图相对于另一个视图上、左、下、右的距离

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

    return UIEdgeInsetsMake(10,8,10,8);

}

 

 

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

       

转载于:https://www.cnblogs.com/huanghsh/p/4275817.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值