在storyboard中创建collectionView-添加头

752372-20160426163749798-17047030.png

752372-20160426164237080-462284572.png

collectionView的模拟创建.zip

#import "FindProductNew.h"
#import "SHStoreCollectionCell.h"
#import "HeaderSectionView.h"

@interface FindProductNew ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (weak, nonatomic) IBOutlet UICollectionView *rightCollection; //创建collection

@end

@implementation FindProductNew

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];


#pragma 配置文件
    self.rightCollection.delegate = self;
    self.rightCollection.dataSource = self;
#warning 在storyboard中,不能使用下面的代码,否则cell中的内容都不显示
//    [self.rightCollection registerClass:[SHStoreCollectionCell class] forCellWithReuseIdentifier:@"SHStoreCollectionCell"];
 }

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

    HeaderSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderSectionView" forIndexPath:indexPath];
    return headerView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

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

    return 10;

}

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

#pragma mark 在storyboard中重用identifier与这个要一致
    static  NSString * identifierCell = @"SHStoreCollectionCell";
    SHStoreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];

    cell.placeLabel.text = @"张家界";
    cell.backgroundColor = [UIColor whiteColor];

    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建九宫格的步骤如下: 1. 创建一个新的 Xcode 项目,选择 Single View Application。 2. 在 Main.storyboard 添加一个 UICollectionView。在 Attributes Inspector 将 Scroll Direction 设置为 Vertical。 3. 在 UICollectionViewCell 添加一个 UILabel,将其居,并设置它的控制点约束(Control Point Constraints)为 0。 4. 在 Attributes Inspector 将 Cell Size 设置为 100 x 100,Minimum Spacing 设置为 0。 5. 在 ViewController.swift 添加以下代码: ``` import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { @IBOutlet weak var collectionView: UICollectionView! let items = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] override func viewDidLoad() { super.viewDidLoad() collectionView.dataSource = self collectionView.delegate = self } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return items.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell cell.label.text = items[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("You selected cell #\(indexPath.item + 1)") } } class CustomCell: UICollectionViewCell { @IBOutlet weak var label: UILabel! } ``` 6. 在 Main.storyboard 选择 UICollectionViewCell,将其 Class 设置为 CustomCell,并将 Identifier 设置为 cell。 这样,你就可以在模拟器运行你的应用程序,看到一个九宫格了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值