以上为ViewController中的代码
FlagView中的代码:
#import <UIKit/UIKit.h> #import "CZFlag.h" @interface FlagView : UIView @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *nameView; @property (nonatomic, strong) CZFlag *flag; + (instancetype )flagView; @end //.m中 #import "FlagView.h" @implementation FlagView + (instancetype )flagView { return [[[NSBundle mainBundle] loadNibNamed:@"FlagView" owner:nil options:nil] lastObject]; } - (void)setFlag:(CZFlag *)flag { self.nameView.text = flag.name; self.iconView.image = [UIImage imageNamed:flag.icon]; } @endCZFlag中
#import <Foundation/Foundation.h> @interface CZFlag : NSObject @property (nonatomic, copy)NSString *name; @property (nonatomic, copy)NSString *icon; + (NSArray *)flagList; - (instancetype)initWithDic:(NSDictionary *)dic; + (instancetype)flagWithDic:(NSDictionary *)dic; @end //.m中 #import "CZFlag.h" @implementation CZFlag - (instancetype)initWithDic:(NSDictionary *)dic { if (self = [super init]) { [self setValuesForKeysWithDictionary:dic]; } return self; } + (instancetype)flagWithDic:(NSDictionary *)dic { CZFlag *flag = [[CZFlag alloc] initWithDic:dic]; return flag; } + (NSArray *)flagList { NSString *path = [[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"]; NSMutableArray *tmpArray = [NSMutableArray array]; NSArray *dicArray = [NSArray arrayWithContentsOfFile:path]; for (NSDictionary *dic in dicArray) { CZFlag *flag = [CZFlag flagWithDic:dic]; [tmpArray addObject:flag]; } return tmpArray; } @end效果如下
转载于:https://www.cnblogs.com/BJTUzhengli/p/5097422.html