Iphone 每当我的plist更新时,是否尝试重新加载UItableview?

Iphone 每当我的plist更新时,是否尝试重新加载UItableview?,iphone,objective-c,xcode,plist,Iphone,Objective C,Xcode,Plist,我有一个plist,它由我的应用程序中的用户填充。我正试图在UITableView中显示该plist的内容,它在重新启动应用程序之前不会显示,这意味着它不会更新。以下是我所拥有的: - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnV

我有一个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];

}

提前谢谢

据我所知,你应该打电话来

[self.tableView reloadData]

将数据从plist加载到foodArray后,无论何时更新plist文件,只要调用self.tableView reloadData即可。此外,请确保用于UITableView的数据源已更新。此数据源可以是NSDictionary、NSArray、NSMutableArray等。tableView的重载数据方法将触发cellforRowIndexPath方法,您将在其中创建UITableViewCell。

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

@property(nonanatomic,stron) NSMutableArray *arrayFood;
然后您应该覆盖它的setter方法

-(NSMutableArray *)setArrayFood:(NSMutableArray *)array
{
   //set your data array here and simply call reloadData
   [self.tableView reloadData];
}
这样,每当数据数组发生更改时,tableView都会得到更新


还可以使用NSNotificationCenter进行列表更改并在那里更新您的阵列食品。

使用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];
}

表示要将表的内容更改为plist update?无论何时更新plist,都要重新加载表。问题是我要重新加载另一个视图。因此,plist被填充的地方是一个不同于显示它的视图控制器(如果有意义的话)。是否有一种方法可以执行类似于[otherViewController reloadData]的操作,或者在每次单击将我们带到新视图控制器的按钮时重新加载数据?抱歉,这让人困惑