2012-12-18 53 views
0

我有一個由用戶在我的應用程序中填充的plist。我試圖在UITableView中顯示該plist的內容,並且直到應用程序重新啓動後纔會顯示,這意味着它不會更新。這裏是我有:當我的plist更新時試圖重新加載一個UItableview?

- (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]; 

} 

在此先感謝!

+0

表示您希望將表格的內容更改爲plist u更新? –

+0

無論您何時更新您的plist,都可以重新載入表格。 –

回答

0

從我看到,你應該叫

[self.tableView reloadData] 

你的plist加載數據後foodArray在viewDidAppear ...

0

每次更新plist文件只需調用self.tableView reloadData。此外,請確保您用於UITableView的數據源已更新。此數據源可以是NSDictionary,NSArray,NSMutableArray等。tableView的reloadData方法將觸發cellforRowIndexPath方法,您將在其中創建UITableViewCell。

+0

問題是我想重新加載另一個視圖。所以在plist被填充的地方是一個不同的視圖控制器,而不是顯示它的那個視圖控制器。有沒有辦法做到像 [otherViewController reloadData]或重新加載每次他們點擊按鈕,把我們帶到新的視圖控制器?對不起,如果這是令人困惑 – Andy

0

更新與最新的數據的UITableView的最好的辦法是要覆蓋的數據陣列的設置方法。 例如您的數據陣列arrayFood所以你可能有它的強勁性能,或者說保留

@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。

0

使用NSNotificationCenter可能會對您有所幫助。

在此更新您的plist,當這種情況發生更新把這個代碼視圖控制器:

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

在視圖控制器,你會想這個代碼添加到viewDidLoad中:

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

而且,在同一個ViewController中添加此功能:

-(void)plistDidUpdate:(id)sender{ 
    [self.tableView reloadData]; 
}