加载plist文件中的数据,两种方法

方法-:

NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

NSDictionary *drinkDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *array_ = [drinkDictionary objectForKey:@"drinks"];

NSDictionary *dic=[array_ objectAtIndex:index];

NSString *name=[dic objectForKey:@"name"];

        NSArray *arrayStr= [[dic objectForKey:@"material"] componentsSeparatedByString:@"\r"];//将string划分为数组,以换行的形式显示出来

NSMutableString *string_ = [[NSMutableString alloc] initWithString:@""];

for (int i = 0; i < [array_ count]; i++) {

[string_ appendFormat:@"<div style='background:rgba(100, 100, 255, 0) none repeat scroll 0 0 !important; filter:Alpha(opacity=0);width:300;height:24px;'><span  style='position:relative;color:black'>%@</span></div>",[array_ objectAtIndex:i]];

}

[string_ appendString:@""];

materalWebView.frame = CGRectMake(20,490,280, [array_ count] * 40);//UIWebView *materalWebView;

[materalWebView loadHTMLString:string_ baseURL:nil];


加载plist文件中的数据,两种方法_第1张图片
方法二:
加载plist文件中的数据,两种方法_第2张图片

#pragma mark -loaddrinkData

-(NSArray*)loadDinkInfoData

{

    static NSArray *__drinkData = nil; // only load the imageData array once

    if (__drinkData == nil) {

        // read the filenames/sizes out of a plist in the app bundle

        NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkInfoDataList" ofType:@"plist"];

        NSData *plistData = [NSData dataWithContentsOfFile:path];

        NSString *error; NSPropertyListFormat format;

        __drinkData = [[NSPropertyListSerialization propertyListFromData:plistData

                                                        mutabilityOption:NSPropertyListImmutable

                                                                  format:&format

                                                        errorDescription:&error]

                       retain];

        if (!__drinkData) {

            NSLog(@"Failed to read drinkData info. Error: %@", error);

            [error release];

        }

        else NSLog(@"-----drink data=%@",__drinkData);

    }

    return __drinkData;

}

- (NSUInteger)drinkDataCount {

    static NSUInteger __count = NSNotFound// only count the data once

    if (__count == NSNotFound) {

        __count = [[self loadDinkInfoData] count];

    }

    return __count;

}

- (NSString *)drinkNameAtIndex:(NSUInteger)index {

    NSString *name = nil;

    if (index < [self drinkDataCount]) {

        NSDictionary *data = [[self loadDinkInfoData] objectAtIndex:index];

        name = [data valueForKey:@"name"];

    }

    return name;

}

- (NSArray *)drinkComponentAtIndex:(NSUInteger)index {

    NSArray *_component = nil;

    if (index < [self drinkDataCount]) {

        NSDictionary *data = [[self loadDinkInfoData] objectAtIndex:index];

        _component = [data valueForKey:@"component"];

    }

    return _component;

}

- (NSString *)drinkMessageAtIndex:(NSUInteger)index {

    NSString *message = nil;

    if (index < [self drinkDataCount]) {

        NSDictionary *data = [[self loadDinkInfoData] objectAtIndex:index];

        message = [data valueForKey:@"message"];

    }

    return message;

}

//image

- (UIImage *)imageAtIndex:(NSUInteger)index {

    // use "imageWithContentsOfFile:" instead of "imageNamed:" here to avoid caching our images

    NSString *imageName = [self imageNameAtIndex:index];

    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];

    return [UIImage imageWithContentsOfFile:path];    

}


- (NSString *)imageNameAtIndex:(NSUInteger)index {

    NSString *imagename = nil;

    if (index < [self drinkDataCount]) {

        NSDictionary *data = [[self loadDinkInfoData] objectAtIndex:index];

        imagename = [data valueForKey:@"imagename"];

    }

    return imagename;

}

显然第二种方法比较整齐,但有些罗嗦。。。万变不离其中

你可能感兴趣的:(String,Path,div,scroll,caching)