iOS开发:表格与集合视图及位置服务实现指南
立即解锁
发布时间: 2025-08-25 01:37:23 阅读量: 2 订阅数: 20 


iOS 7开发实战:从入门到精通
### iOS开发:表格与集合视图及位置服务实现指南
#### 集合视图用户界面完善
在完成集合视图用户界面的定义之前,还需要设置单元格和补充视图的大小。这可以在 `collectionView:collectionViewLayout:sizeForItemAtPath:` 和 `collectionView:collectionViewLayout:referenceSizeForHeaderInSection:` 数据源方法中完成,示例代码如下:
```objc
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)
collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 75);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)
collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(50, 50);
}
```
需要注意的是,在集合视图流布局中设置页眉或页脚的大小时,只考虑一个维度。例如,如果是垂直流布局,只使用 `CGSize` 的高度分量来确定补充视图的实际大小,宽度则由集合视图的宽度推断得出。水平流布局则相反,只考虑提供的宽度。
#### 国旗选择器功能实现
在国旗选择器中,最后一项任务是添加代码,以便在选择国旗时通知主视图。这可以通过在 `FlagPickerViewController` 中添加委托方法来实现,示例代码如下:
```objc
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)
indexPath
{
Flag *selectedFlag = [self flagForIndexPath:indexPath];
[self.delegate flagPicker:self didPickFlag:selectedFlag];
}
```
#### 显示国旗选择器
要使用刚刚构建的国旗选择器,首先需要将主视图控制器设置为国旗选择器的委托。这可以通过让主视图控制器符合之前定义的 `FlagPickerViewControllerDelegate` 协议来实现。在 `ViewController.h` 中添加以下代码:
```objc
//
// ViewController.h
// Recipe 4-6 Creating a flag picker collection view
//
#import <UIKit/UIKit.h>
#import "FlagPickerViewController.h"
@interface ViewController : UIViewController<FlagPickerViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *countryLabel;
@property (weak, nonatomic) IBOutlet UIImageView *flagImageView;
- (IBAction)pickFlag:(id)sender;
@end
```
然后在 `ViewController.m` 中实现 `pickFlag:` 动作方法:
```objc
- (IBAction)pickFlag:(id)sender
{
UICollectionViewController *flagPicker =
[[FlagPickerViewController alloc] initWithDelegate:self];
[self presentViewController:flagPicker animated:YES completion:NULL];
}
```
最后,添加响应国旗选择器选择事件的方法:
```objc
-(void)flagPicker:(FlagPickerViewController *)flagPicker didPickFlag:(Flag *)flag
{
self.flagImageView.image = flag.image;
self.countryLabel.text = flag.name;
[self dismissViewControllerAnimated:YES completion:NULL];
}
```
此时,你可以构建并运行应用程序。点击“Change Flag”按钮,应该会显示一个类似于图4 - 20的视图。你可以在国旗之间滚动并选择一个,所选国旗将用于更新主视图。
#### 解决旋转问题
当旋转设备并激活国旗选择器时,会发现部分标题不再居中。这是因为标题标签是使用固定大小创建的,在竖屏模式下可以正常工作,但屏幕旋转时,静态大小的标签不会随之调整。为了解决这个问题,可以使用自动布局。在 `ContinentHeader.m` 的 `initWithFrame:` 方法中添加以下代码:
```objc
//
// ContinentHeader.m
// Recipe 4-6 Creating a flag picker collection view
//
#import "ContinentHeader.h"
@implementation ContinentHeader
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.label = [[UILabel alloc] initWithFrame:
CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.label.font = [UIFont systemFontOfSize:20];
self.label.textColor = [UIColor whiteColor];
self.label.backgroundColor = [UIColor clearColor];
self.label.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.label];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary =
```
0
0
复制全文
相关推荐










