大綱
一、字典NSDictionary
項(xiàng)目:Dictionary0321
無序存儲的,通過鍵值對的形式存在。
- NSDictionary
1.創(chuàng)建并初始化
2.獲取 - NSMutableDictionary
1.創(chuàng)建并初始化
2.獲取、增加、修改、刪除
小提示:
NSNumber對象 將基本數(shù)據(jù)類型轉(zhuǎn)換為 對象類型
作用:將基本數(shù)據(jù)類型作為對象來操作。
NSNumber *number = [NSNumber numberWithInt:1];
二、選擇器UIPickerView
版本1(1維數(shù)組)
項(xiàng)目:PickerView0321
NSAttributedString:可以設(shè)置大小、顏色(NSString就不行)
步驟:準(zhǔn)備數(shù)據(jù)(一維數(shù)組)
創(chuàng)建UIPickerView
設(shè)置代理
3.1 設(shè)置 行、列 標(biāo)題
3.2 刷新數(shù)據(jù)設(shè)置數(shù)據(jù)源
4.1 設(shè)置列數(shù)
4.2 設(shè)置行數(shù)添加到self.view
版本2(數(shù)組嵌套【相當(dāng)于C語言中的二維數(shù)組】)
項(xiàng)目:PickerView_Arr&Arr0321
步驟:準(zhǔn)備數(shù)據(jù)(使用數(shù)組嵌套數(shù)組)
創(chuàng)建UIPickerView
設(shè)置代理
3.1 設(shè)置 行、列 標(biāo)題
3.2 刷新數(shù)據(jù)設(shè)置數(shù)據(jù)源
4.1 設(shè)置列數(shù)
4.2 設(shè)置行數(shù)添加到self.view
版本3(字典)
項(xiàng)目:PickerView_Dictionary0321
步驟:準(zhǔn)備數(shù)據(jù)(字典存數(shù)組)
創(chuàng)建UIPickerView
設(shè)置代理
3.1 設(shè)置 行、列 標(biāo)題
3.2 刷新數(shù)據(jù)設(shè)置數(shù)據(jù)源
4.1 設(shè)置列數(shù)
4.2 設(shè)置行數(shù)添加到self.view
三、時間選擇器UIDatePicker
項(xiàng)目:DatePicker0321UIDatePicker
步驟:
1.創(chuàng)建初始化
2.設(shè)置屬性
3.添加事件
4.添加到self.viewNSDateFormatter
步驟:
1.創(chuàng)建初始化
2.確定格式(setDateFormat)
3.將時間,按上述格式格式化
正文
一、字典NSDictionary
項(xiàng)目:Dictionary0321
無序存儲的,通過鍵值對的形式存在。
- NSDictionary
1.創(chuàng)建并初始化
2.獲取 - NSMutableDictionary
1.創(chuàng)建并初始化
2.獲取、增加、修改、刪除
小提示:
NSNumber對象 將基本數(shù)據(jù)類型轉(zhuǎn)換為 對象類型
作用:將基本數(shù)據(jù)類型作為對象來操作。
NSNumber *number = [NSNumber numberWithInt:1];
源碼:
//1.不可變字典:NSDictionary
//1.1 創(chuàng)建
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"鄭州",@"河南",@"石家莊",@"河北",@"南京",@"江蘇", nil];
//1.2 根據(jù) 鍵 獲取字典中相對應(yīng)的值
NSString *str = [dic objectForKey:@"江蘇"];
// NSLog(@"%@",str);
//1.3 遍歷
//獲取所有的Key
NSArray *arrKeys = [dic allKeys];
for (int i = 0; i < arrKeys.count; i++)
{
NSString *provinceKey = [arrKeys objectAtIndex:i];
NSString *cityValue = [dic objectForKey:provinceKey];
// NSLog(@"city = %@",cityValue);
}
//2.可變字典:NSMutableDictionary
//2.1 創(chuàng)建
NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc]init];
//2.2 增加 鍵-值 對
[mutableDic setObject:@"張三" forKey:@"鍵 1"];
[mutableDic setObject:@"李四" forKey:@"鍵 2"];
//2.3 修改 修改 鍵 所對應(yīng)的 值
[mutableDic setObject:@"小王" forKey:@"鍵 1"];
//2.4 刪除
[mutableDic removeObjectForKey:@"鍵 1"];
// [mutableDic removeAllObjects];
// [mutableDic removeObjectsForKeys:(nonnull NSArray *)];
//2.5 遍歷
NSArray *arrKeys2 = [mutableDic allKeys];
for (int i = 0; i < arrKeys2.count; i++)
{
NSString *key = [arrKeys2 objectAtIndex:i];
NSString *value = [mutableDic objectForKey:key];
NSLog(@"Key = %@",key);
NSLog(@"Value = %@",value);
}
二、選擇器UIPickerView
- 版本1(1維數(shù)組)
項(xiàng)目:PickerView0321
NSAttributedString:可以設(shè)置大小、顏色(NSString就不行)
步驟: - 準(zhǔn)備數(shù)據(jù)(一維數(shù)組)
- 創(chuàng)建UIPickerView
- 設(shè)置代理
3.1 設(shè)置 行、列 標(biāo)題
3.2 刷新數(shù)據(jù) - 設(shè)置數(shù)據(jù)源
4.1 設(shè)置列數(shù)
4.2 設(shè)置行數(shù) - 添加到self.view
源碼:
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
NSArray *_provinceArr,*_heNanArr,*_heBeiArr,*_shanDongArr,*_guangDongArr;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//提前準(zhǔn)備好數(shù)據(jù),放在數(shù)組中
_provinceArr = [[NSArray alloc]initWithObjects:@"河南",@"河北", nil];
_heNanArr = [[NSArray alloc]initWithObjects:@"鄭州",@"開封",@"洛陽",@"平頂山",@"信陽",@"漯河",@"周口", nil];
_heBeiArr = [[NSArray alloc]initWithObjects:@"石家莊",@"邯鄲",@"保定",@"唐山",@"秦皇島", nil];
//1.選擇器
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 300, 100)];
picker.backgroundColor = [UIColor redColor];
//1.1 協(xié)議方法
//設(shè)置代理
picker.delegate = self;
//設(shè)置數(shù)據(jù)源
//dataSource也是代理模式
picker.dataSource = self;
[self.view addSubview:picker];
}
#pragma mark - UIPickerViewDataSource
//設(shè)置列數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//設(shè)置行數(shù)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//component表示列號
if (component == 0)
{
return _provinceArr.count;
}
else
{
//首先確定 第0列 選中的行號
int rowNum = [pickerView selectedRowInComponent:0];
if (rowNum == 0)
{
return _heNanArr.count;
}
else
{
return _heBeiArr.count;
}
}
}
#pragma mark - UIPickerViewDataSource
//刷新數(shù)據(jù)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//選中第0列 刷新第1列
if (component == 0)
{
//重新加載數(shù)據(jù)
[pickerView reloadComponent:1];
//讓第1列 從0行開始 顯示
[pickerView selectRow:0 inComponent:1 animated:YES];
}
}
//設(shè)置 每行、每列 的標(biāo)題
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
{
//根據(jù)行號 獲取標(biāo)題
return [_provinceArr objectAtIndex:row];
}
else
{
//首先 確定 第0列 選擇的 行號
int rowNum = [pickerView selectedRowInComponent:0];
//設(shè)置 第1列 每行的 標(biāo)題
if (rowNum == 0)
{
return [_heNanArr objectAtIndex:row];
}
else
{
return [_heBeiArr objectAtIndex:row];
}
}
}
- 版本2(數(shù)組嵌套【相當(dāng)于C語言中的二維數(shù)組】)
項(xiàng)目:PickerView_Arr&Arr0321
步驟: - 準(zhǔn)備數(shù)據(jù)(使用數(shù)組嵌套數(shù)組)
- 創(chuàng)建UIPickerView
- 設(shè)置代理
3.1 設(shè)置 行、列 標(biāo)題
3.2 刷新數(shù)據(jù) - 設(shè)置數(shù)據(jù)源
4.1 設(shè)置列數(shù)
4.2 設(shè)置行數(shù) - 添加到self.view
源碼(與一維數(shù)組的區(qū)別):
//1.組織數(shù)據(jù)
_provinceArr = [[NSArray alloc]initWithObjects:@"河南",@"山東",@"陜西",@"安徽", nil];
NSArray *heNanArr = [[NSArray alloc]initWithObjects:@"鄭州",@"平頂山",@"南陽", nil];
NSArray *shanDongArr = [[NSArray alloc]initWithObjects:@"濟(jì)南",@"日照",@"菏澤",@"煙臺", nil];
//1.1 創(chuàng)建另一個數(shù)組,存放城市數(shù)組
_totalArr = [[NSArray alloc]initWithObjects:heNanArr,shanDongArr, nil];
- 版本3(字典)
項(xiàng)目:PickerView_Dictionary0321
步驟: - 準(zhǔn)備數(shù)據(jù)(字典存數(shù)組)
- 創(chuàng)建UIPickerView
- 設(shè)置代理
3.1 設(shè)置 行、列 標(biāo)題
3.2 刷新數(shù)據(jù) - 設(shè)置數(shù)據(jù)源
4.1 設(shè)置列數(shù)
4.2 設(shè)置行數(shù) - 添加到self.view
源碼:
//1.通過 字典 組織數(shù)據(jù)
NSArray *heNanArr = [[NSArray alloc]initWithObjects:@"鄭州",@"平頂山",@"南陽", nil];
NSArray *shanDongArr = [[NSArray alloc]initWithObjects:@"濟(jì)南",@"日照",@"菏澤",@"煙臺", nil];
//創(chuàng)建字典
_provinceDic = [[NSDictionary alloc]initWithObjectsAndKeys:heNanArr,@"河南省",shanDongArr,@"山東省",shanXiArr, nil];
三、時間選擇器UIDatePicker
項(xiàng)目:DatePicker0321
UIDatePicker
步驟:
1.創(chuàng)建初始化
2.設(shè)置屬性
3.添加事件
4.添加到self.viewNSDateFormatter
步驟:
1.創(chuàng)建初始化
2.確定格式(setDateFormat)
3.將時間,按上述格式格式化
源碼:
- (void)viewDidLoad
{
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 320, 240)];
datePicker.datePickerMode = UIDatePickerModeDate;
//獲取當(dāng)前 最小的 日期時間
datePicker.minimumDate = [NSDate date];
//獲取從現(xiàn)在起 間隔多長時間
//參數(shù)單位:秒
datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:31*24*60*60];
//添加點(diǎn)擊事件
[datePicker addTarget:self action:@selector(selectDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
//點(diǎn)擊時間
- (void)selectDate:(UIDatePicker *)datePicker
{
//獲取選中的時間
NSDate *selectedDate = [datePicker date];
//**************日期格式化**************
//1.創(chuàng)建 日期格式化器
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//2.設(shè)置日期格式
//1999年12月1日 12點(diǎn)11分5秒 1999-12-1 12:11:5
//yyyy-MM-dd
//hh:mm:ss 12小時制
//hh:mm:ss a 12小時制(a:上下午)
//HH:mm:ss 24小時制
[dateFormatter setDateFormat:@"yyyy年MM月dd日 hh時mm分ss秒 a"];
// [dateFormatter setAMSymbol:@"上午好"];
[dateFormatter setPMSymbol:@"下午好"];
//3.將格式化的日期,轉(zhuǎn)換為字符串
NSString *dateStr = [dateFormatter stringFromDate:selectedDate];
NSLog(@"%@",dateStr);
_labDate.text = dateStr;
}