标签:
1 委托对象负责控制控件的外观和对控件的事件和状态作出反应
数据源对象是控件与应用数据(model)的桥梁,一般是必须实现的。
2 选择器 UIPickerView 为用户提供选择
1)日期选择器 UIDatePicker
设置属性检查器中的各个属性-代码
-setDateFormat:设置日期格式
-stringFromDate:获取时间
- (IBAction)onclick:(id)sender {
NSDate * theDate = self.datePicker.date;获取选中日期
NSLog(@"the date picked is: %@", [theDate descriptionWithLocale:[NSLocale currentLocale]]);返回基于本地化的日期信息
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;日期格式
dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";-setDateFormat:
NSLog(@"the date formate is: %@", [dateFormatter stringFromDate:theDate]);
self.label.text = [dateFormatter stringFromDate:theDate];
}
2)普通选择器UIpickerView dateSource和delegate
**************
viewDidLoad:中是要准备默认(当前的显示数据),设置代理
**************
获取数据 :获取主目录 +mainBundle
获取资源路径 -pathForResource:ofType:
获取数据 -initWithContentsOfFile:
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"provinces_cities"
ofType:@"plist"];
//获取属性列表文件中的全部数据
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.pickerData = dict;
//省份名数据
self.pickerProvincesData = [self.pickerData allKeys];从字典中拿取全部key值
//默认取出第一个省的所有市的数据
NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:0];
self.pickerCitiesData = [self.pickerData objectForKey:seletedProvince];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
*********
一个key多个value值可以将value 值放入array中,再组成字典。
取出所有key-取出第一个key-取出这个key对应的value。
*********
动作方法:
-selectedRowInComponent:返回被选中拨盘的行数
- (IBAction)onclick:(id)sender {
NSInteger row1 = [self.pickerView selectedRowInComponent:0];在第一个拨盘中的行数
NSInteger row2 = [self.pickerView selectedRowInComponent:1];
NSString *selected1 = [self.pickerProvincesData objectAtIndex:row1];根据行数取数据
NSString *selected2 = [self.pickerCitiesData objectAtIndex:row2];
NSString *title = [[NSString alloc] initWithFormat:@"%@,%@市",
selected1,selected2];
self.label.text = title;
}
实现代理协议:
dataSource:
- numberOfComponentsInPickerView:返回拨盘数
-pickerView:numberOfRowsInComponent:返回每个拨盘的行数
delegate:
-pickerView:titleForRow:forComponent:为拨盘中的行提供显示数据 显示到某行时调用
-pickerView:didSelectRow:inComponent:选中行时调用
-reloadComponent:刷新拨盘并回调显示方法
#pragma mark 实现协议UIPickerViewDataSource方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {//省份个数
return [self.pickerProvincesData count];
} else {//市的个数
return [self.pickerCitiesData count];
}
}
#pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {//选择省份名
return [self.pickerProvincesData objectAtIndex:row];
} else {//选择市名
return [self.pickerCitiesData objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:row];
NSArray *array = [self.pickerData objectForKey:seletedProvince];
self.pickerCitiesData = array;
[self.pickerView reloadComponent:1];此时会回调上面的显示方法
}
}
3 集合视图(网格视图)UICollectionView
结构:节,单元格(UICollectionViewCell类,布局由UICollectionViewLayout类定义),补充视图,装饰视图,控制器(UICollectionViewController)
准备:添加资源-删除viewController-添加collectionViewController到设计界面并设为Is Initial View Controller-修改viewController类为UICollectionViewController类并与界面关联-适配屏幕
设计并添加单元格:单元格就是一个视图,可以在其内部放置其他视图或控件。
新建单元格类并和设计界面的Collection View Cell关联起来-在单元格属性检查器中设置可重用单元格标示-在单元格尺寸检查器中设置单元格大小-设置Collection View尺寸检查器中的Cell Size值-在Cell视图中自定义吧-添加约束
@interface Cell : UICollectionViewCell 自定义Cell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *label;
实现委托协议:
dataSource:
-numberOfSectionInCollectionView:节数目
-collectionView:numberOfItemInSection:节中的列数目
-collectionView:cellForItemAtIndexPath:为单元格提供显示数据
-collectionView:viewForSupplementaryElementOfkind:atIndexPath:为补充视图提供显示数据
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"events"
ofType:@"plist"];
//获取属性列表文件中的全部数据
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
self.events = array;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [self.events count] / 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ 重用机制
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)];
cell.label.text = [event objectForKey:@"name"];
cell.imageView.image = [UIImage imageNamed:[event objectForKey:@"image"]];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)];
NSLog(@"select event name : %@", [event objectForKey:@"name"]);
}
IOS 开发指南 第5章 委托协议 数据源协议 高级视图学习
标签:
原文地址:http://www.cnblogs.com/haugezi/p/4817731.html