iOS UICollectionView 入门 06 添加标题view

新建继承于UICollectionReusableView的类FlickrPhotoHeaderView,添加以下两个outlet:

@interface FlickrPhotoHeaderView()@property (weak) IBOutlet UIImage *backgroundImageView;@property (weak) IBOutlet UILabel *searchLabel;@end

打开storyboard,,选中CollectionView,通过Attributes Inspector使能Section Header:

添加一个image view和一个label到header上:

选中reusable view,设置其class位FlickrPhotoHeaderView,设置Identifier为FlickrPhotoHeaderView。

接下来,我们通过代码让collection view使用header view:

– (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{FlickrPhotoHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FlickrPhotoHeaderView" forIndexPath:indexPath];NSString *searchTerm = self.searchs[indexPath.section];[headerView setSearchText:searchTerm];return headerView;}为FlickrPhotoHeaderView添加setSearchText方法:@implementation FlickrPhotoHeaderView- (void)setSearchText:(NSString *)text{self.searchLabel.text = text;UIImage *shareButtonImage = [[UIImage imageNamed:@"header_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(68, 68, 68, 68)];self.backgroundImageView.image = shareButtonImage;}编译执行:

下一节我们将为程序添加单击图片放大功能。

遇见你,是我一生的幸运;爱上你,

iOS UICollectionView 入门 06 添加标题view

相关文章:

你感兴趣的文章:

标签云: