今天在使用UITableView时遇到的问题,我在plist文件配置的数据进不去列表,以下是解决方案
问题原因:plist文件root的type配置错误
如上图所示,博主是使用plist文件作为我的沙盒,if Root's Type 为array时
self.simpleData = [[NSArray alloc] initWithContentsOfFile:filePath]; self.simpleData=[NSArray arrayWithContentsOfFile:filePath];
上面的第一行与第三行代码任选其一就可以
如果type为Dictionary时
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
利用这种方式将plist文件位置传进去就行。
附带源码
// // ViewController.h // Table // // Created by snow on 2017/12/25. // Copyright © 2017年 snow. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UITableViewController @property (strong,nonatomic)NSArray *simpleData; @end
// // ViewController.m // Table // // Created by snow on 2017/12/25. // Copyright © 2017年 snow. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"simpleData" ofType:@"plist"]; //self.simpleDatas=[NSMutableArray arrayWithContentsOfFile:filePath]; self.simpleData = [[NSArray alloc] initWithContentsOfFile:filePath]; // self.simpleData=[NSArray arrayWithContentsOfFile:filePath]; // NSMutableDictionary *data = [[NSMutableDictionary // alloc] // initWithContentsOfFile:filePath]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.simpleData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *identifierString = @"simpleTableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString]; } NSInteger rowIndex = [indexPath row]; NSDictionary *cellDictionary = [self.simpleData objectAtIndex:rowIndex]; cell.textLabel.text = [cellDictionary objectForKey:@"name"]; cell.imageView.image = [UIImage imageNamed:[cellDictionary objectForKey:@"cover"]]; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; return cell; } @end