标签栏与选取器

来源:互联网 发布:淘宝的虾米vip会员 编辑:程序博客网 时间:2024/05/20 23:34

1、日期选取器

      使用UIDatePicker控件,[datePicker date]函数将返回其被选取的日期;

2、单组件选取器

      使用UIPickerView控件,在其视图中,需要将Delegate拖到file‘s owner中,DataSource不需要(为什么DataSource不要呢?目前我还不是很了解)。[pickerData ObjectAtIndex:i]函数将返回第i行的数据,[pickerData selectedRowInComponent:0]返回第一列中被选中的行数,需要定义一个数组用来存储每一列中的数据,self.pickData = array,将数组中的元素传递给选取器。

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberofComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;//定义选取器中的列数
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
    numberofRowsInComponent:(NSInteger)component{
    return [pickerData count];//询问给定的选取器中包含多少行
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
    titleForRow:(NSInteger)row
             forComponent:(NSInteger)component{
    return [pickerData objectAtIndex:row];//书上说想要在选取器中显示除文本以外的内容,必须实现这个方法,不太明白
}

3、多组件选取器

     两列的话,需要定义选取器列数时,return 2;每一列都需要一个数组来存取数据,通过0和1来标识是哪一列。[pickData selectedRowInComponent:0]返回第一列被选中的行数。

     数据源中需要判断选取的是哪一列的数据

- (NSInteger)pickerView:(UIPickerView *)pickerView
    numberofRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return [self.array1 count];
    }
    return [self.array2 count];
}

4、依赖组件选取器

     需要用到字典,如何自己产生字典,暂不清楚,直接将字典导进去的。

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:
                           @"statdictionary" ofType:@"plist" ];//该字典以洲名作为键
    NSDictionary *dictionary = [[NSDictionary alloc]
                                initWithContentsOfFile:plistPath];
    NSArray *component = [self.dictionary allKeys];
    NSArray *sorted = [component sortedArrayUsingSelector:
                       @selector(compare:)];//对获取的键列表按字母顺序进行排序
    NSArray *selectedState = [self.sorted objectAtIndex:0];//从每个洲数组中获取索引为0的对象
    NSArray *array = [dictionary objectForKey:selectedState];//从字典中提取数组的邮政编码

5、自定义选取器

      将组件选取器设置为多行。
原创粉丝点击