自定义控件-PickerView
来源:互联网 发布:linux yum命令有什么用 编辑:程序博客网 时间:2024/05/01 08:59
#import "ViewController.h"#import "QHFlag.h"#import "QHFlagView.h"@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>@property (nonatomic, strong) NSArray *flags;@end@implementation ViewController- (NSArray *)flags{ if (!_flags) { NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil]]; NSMutableArray *flagArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { QHFlag *flag = [QHFlag flagWithDict:dict]; [flagArray addObject:flag]; } _flags = flagArray; } return _flags;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return self.flags.count;}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //1.创建flagView QHFlagView *flagView = [QHFlagView flagViewWithResuingView:view]; flagView.flag = self.flags[row]; return flagView; }- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return [QHFlagView flagViewHeight];}
数据模型
#import <Foundation/Foundation.h>@interface QHFlag : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *icon;+ (instancetype)flagWithDict:(NSDictionary *)dict;- (instancetype)initWithDict:(NSDictionary *)dict;@end
#import "QHFlag.h"@implementation QHFlag+ (instancetype)flagWithDict:(NSDictionary *)dict{ return [[self alloc]initWithDict:dict];}- (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self;}@end
#import <UIKit/UIKit.h>@class QHFlag;@interface QHFlagView : UIView@property (nonatomic, strong) QHFlag *flag;+ (instancetype)flagViewWithResuingView:(UIView *)resuingView;+ (CGFloat)flagViewHeight;@end
#import "MJFlagView.h"#import "QHFlag.h"@interface MJFlagView()@property (weak, nonatomic) IBOutlet UILabel *nameLabel;@property (weak, nonatomic) IBOutlet UIImageView *iconView;@end@implementation QHFlagView+ (instancetype)flagViewWithResuingView:(UIView *)resuingView{ if (resuingView == nil) { return [[[NSBundle mainBundle] loadNibNamed:@"MJFlagView" owner:nil options:nil] lastObject]; } else { return (QHFlagView *)resuingView; }}- (void)setFlag:(QHFlag *)flag{ _flag = flag; // 1.名称 self.nameLabel.text = flag.name; // 2.图标 self.iconView.image = [UIImage imageNamed:flag.icon];}+ (CGFloat)flagViewHeight{ return 44;}@end
0 0
- 自定义控件-PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件_垂直滚动器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- Android自定义控件实战—滚动选择器PickerView
- Android自定义控件实战——滚动选择器PickerView
- iOS 自定义PickerView
- 工具类:自定义 pickerView
- 自定义pickerview实现倒计时
- Android自定义控件实战—滚动选择器PickerView(滚动变化字体)
- pickerView控件的简单使用
- (三十一)PickerView自定义视图
- 计算机编程推荐网站
- Adapter中getItemViewType和getViewTypeCount设置错误产生异常
- 5个典型的JavaScript面试题
- 微软公司等数据结构+算法面试100题(第1-100题)全部出炉
- visual studio中 用ASP.NET实现 增删改功能实现常见异常及处理
- 自定义控件-PickerView
- leetcode之Factorial Trailing Zeroes
- 离圆心最远的整数点(微软笔试题)
- strcpy与memcpy的区别
- 微软等数据结构+算法面试100题全部答案集锦
- android:theme和app:popupTheme的作用,以及在android 3.0以下不起作用问题的解决
- PS混合模式
- C++模板
- JavaScript Array 对象splice() 方法