0

我有一个由用户在我的应用程序中填充的 plist。我正在尝试在 UITableView 中显示该 plist 的内容,直到应用程序重新启动后才会显示,这意味着它不会更新。这是我所拥有的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

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

    arrayFood = [[NSMutableArray alloc]initWithContentsOfFile:path];


}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     [self.tableView reloadData];   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"foodList" ofType:@"plist"];

    arrayFood = [[NSMutableArray alloc]initWithContentsOfFile:path];

}

提前致谢!

4

4 回答 4

0

据我所知,你应该打电话

[self.tableView reloadData]

将数据从 plist 加载到 viewDidAppear 中的 foodArray 后...

于 2012-12-18T02:08:15.963 回答
0

每当您更新 pList 文件时,只需调用 self.tableView reloadData。此外,请确保您用于 UITableView 的数据源已更新。此数据源可以是 NSDictionary、NSArray、NSMutableArray 等。tableView 的 reloadData 方法将触发 cellforRowIndexPath 方法,您将在其中创建 UITableViewCell。

于 2012-12-18T03:23:37.467 回答
0

用最新数据更新 UITableView 的最佳方法是覆盖数据数组的 setter 方法。例如,您的数据数组是 arrayFood,因此您可能将其属性设置为 strong 或保留

@property(nonanatomic,stron) NSMutableArray *arrayFood;

那么你应该覆盖它的setter方法。

-(NSMutableArray *)setArrayFood:(NSMutableArray *)array
{
   //set your data array here and simply call reloadData
   [self.tableView reloadData];
}

这样,每当您的数据数组更改时,您的 tableView 总是会得到更新。

还使用 NSNotifications 进行 plist 更改并在那里更新您的 arrayFood。

于 2012-12-18T05:42:10.553 回答
0

在这里使用 NSNotificationCenter 可能会对您有所帮助。

在更新您的 plist 的 ViewController 中,在此更新发生时放置此代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"plistDidUpdate" object:self];

在 ViewController 中,您需要将此代码添加到 viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(plistDidUpdate:) name:@"plistDidUpdate" object:nil];

另外,在同一个 ViewController 中添加这个函数:

-(void)plistDidUpdate:(id)sender{
    [self.tableView reloadData];
}
于 2013-04-22T01:06:20.227 回答