0

アプリ内でユーザーが設定した 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];

}

前もって感謝します!

4

4 に答える 4

0

私が見るものから、あなたは電話するべきです

[self.tableView reloadData]

viewDidAppearでplistからfoodArrayにデータをロードした後...

于 2012-12-18T02:08:15.963 に答える
0

pList ファイルを更新するときはいつでも、self.tableView reloadData を呼び出すだけです。また、UITableView に使用しているデータ ソースが更新されていることを確認してください。このデータソースは、NSDictionary、NSArray、NSMutableArray などにすることができます。tableView の reloadData メソッドは、UITableViewCell を作成する cellforRowIndexPath メソッドをトリガーします。

于 2012-12-18T03:23:37.467 に答える
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 に答える
0

UITableView を最新のデータで更新する最善の方法は、データ配列のセッター メソッドを上書きすることです。たとえば、データ配列はarrayFoodであるため、そのプロパティをstrongまたはretainと言うことができます

@property(nonanatomic,stron) NSMutableArray *arrayFood;

次に、そのセッター メソッドを上書きする必要があります。

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

そうすれば、データ配列が変更されるたびに、tableView が常に更新されます。

また、plist の変更には NSNotifications を使用し、そこで arrayFood を更新します。

于 2012-12-18T05:42:10.553 に答える