UIImageView From Plist



我设置了一个表,该表从plist文件接收数据,一旦您点击表视图单元格,它就会将您带到显示内容的视图控制器。我希望在从plist加载的视图控制器的顶部有一个UIImageView
我该怎么做呢?

您可以像这样检索plist。

NSString *path = [[NSBundle mainBundle] pathForResource:@"Name" ofType:@"plist"]; 
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary  allValues];

实现所需内容的正确方法取决于plist包含的信息假设您的plist只是图像资产的路径列表,您可以将以下内容添加到有问题的视图控制器的viewDidLoad功能中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"nameOfPList" ofType:@"plist"];
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:plistPath];
    NSString *pathToFile = [contentArray objectAtIndex:0]; // Assumes plist contains paths to images
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToFile];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView]; // Add the newly instantiated UIImageView to the controller's view
}

最新更新