1

これが再投稿である場合は、申し訳ありませんが、ネットを精査しており、機能するものを見つけることができないようです. バンドル内の plists に集められたテーブルビューに表示するワークアウトのリストがあります。ユーザーが独自のワークアウトを作成し、それらをドキュメント フォルダーの plist ファイルに保存できる別のタブもあります。保存すると、テーブル ビューに追加されます。シミュレーターでは、すべて正常に動作します。しかし、実際のデバイスでは、プログラムを長時間閉じるか、xcode からプログラムをリロードするか、電話の電源を切らない限り、更新されません。[[self tableview] reload] を「viewDidLoad」、「viewWillappear」、および「viewDidAppear」に追加しようとしましたが、どれも機能しません。もう一度、ファイルが保存され、シミュレーターで更新が機能しますが、そうではありません。t 電話ですぐに更新します。助言がありますか?ありがとう。

編集:私はそれが長いコードであることを知っていますが、簡単である必要があります(うまくいけば笑)

#import "BIDWODList.h"
#import "BIDWODDetails.h"

#define kFileName   @"SavedDFWorkouts.plist"

@interface BIDWODList ()

@end

@implementation BIDWODList
@synthesize names;
@synthesize savedNames;
@synthesize keys;
@synthesize details;
@synthesize wodType;
@synthesize benchmarkGirls;
@synthesize theNewGirls;    
@synthesize heroes;
@synthesize savedDFGWorkouts;
@synthesize chosenWOD;
@synthesize chosenDetails;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *buildBenchmarkGirls = [[NSMutableArray alloc] init];
    NSMutableArray *buildTheNewGirls = [[NSMutableArray alloc] init];
    NSMutableArray *buildHeroes = [[NSMutableArray alloc] init];

    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistURL = [bundle URLForResource:@"CrossfitWOD" withExtension:@"plist"];
    //put the contents of the plist into a NSDictionary, and then into names instance variable
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
    self.names = dictionary;
    //take all the keys in the dictionary and make an array out of those key names
    self.keys = [self.names allKeys];

    for (NSString *nameCheck in keys){
        self.details = [names valueForKey:nameCheck];
        if ([[self.details valueForKey:@"Type"] isEqualToString:@"The Benchmark Girls"]) {
            [buildBenchmarkGirls addObject:nameCheck];
        }else if ([[self.details valueForKey:@"Type"] isEqualToString:@"The New Girls"]) {
            [buildTheNewGirls addObject:nameCheck];
        }else {
            [buildHeroes addObject:nameCheck];
        }
    }

    NSString *filePath = [self dataFilePath];
    NSMutableDictionary *savedWorkout = [[NSMutableDictionary         alloc]initWithContentsOfFile:filePath];
    self.savedNames = savedWorkout;
    self.savedDFGWorkouts = [[savedWorkout allKeys]     sortedArrayUsingSelector:@selector(compare:)];

    self.benchmarkGirls = [buildBenchmarkGirls sortedArrayUsingSelector:@selector(compare:)];
    self.theNewGirls = [buildTheNewGirls sortedArrayUsingSelector:@selector(compare:)];
    self.heroes = [buildHeroes sortedArrayUsingSelector:@selector(compare:)];
//[[self tableView] reloadData];  //reloads the data in case a DFG workout was saved

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.names = nil;
    self.keys = nil;
    self.benchmarkGirls = nil;
    self.theNewGirls = nil;;
    self.heroes = nil;
    self.details = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSString *)dataFilePath {
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFileName];
}

-(void)viewDidAppear:(BOOL)animated{
    [[self tableView] reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section == 0) {
        return [benchmarkGirls count];
    }else if (section == 1){
        return [theNewGirls count];
    }else if (section == 2){
        return [heroes count];
    }else{
        return [savedDFGWorkouts count];
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier];
    }

    if (section == 0) {
        cell.textLabel.text = [benchmarkGirls objectAtIndex:row];
    }else if (section == 1) {
        cell.textLabel.text = [theNewGirls objectAtIndex:row];
    }else if (section == 2) {
        cell.textLabel.text = [heroes objectAtIndex:row];
    }else{
        cell.textLabel.text = [savedDFGWorkouts objectAtIndex:row];
    }
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @" The Benchmark Girls";
    }else if (section == 1){
        return @"The New Girls";
    }else if (section ==2){
        return @"The Heroes";
    }else{
        return @"Saved DFG Workouts";
    }
}



#pragma mark - Table view delegate


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    BIDWODDetails *destination = segue.destinationViewController;

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
if (section == 0) {
    self.chosenWOD = [self.benchmarkGirls objectAtIndex:row];
    self.chosenDetails = [names objectForKey:chosenWOD];
}else if (section == 1) {
    self.chosenWOD = [self.theNewGirls objectAtIndex:row];
    self.chosenDetails = [names objectForKey:chosenWOD];
}else if (section ==2) {
    self.chosenWOD = [self.heroes objectAtIndex:row];
    self.chosenDetails = [names objectForKey:chosenWOD];
}else {
    self.chosenWOD = [self.savedDFGWorkouts objectAtIndex:row];
    self.chosenDetails = [savedNames objectForKey:chosenWOD];
}//end if

//self.chosenDetails = [names objectForKey:chosenWOD];
//[destination setValue:chosenWOD forKey:@"chosenWOD"];
//[destination setValue:chosenDetails forKey:@"chosenDetails"];
destination.chosenWOD = self.chosenWOD;
destination.chosenDetails = self.chosenDetails;
}

@end
4

3 に答える 3

2

シミュレーターとデバイスの間の動作の違いは、多くの場合、ファイル名で大文字と小文字が正しく使用されていないことに関連しています。シミュレーターでは大文字と小文字が区別されませんが、デバイスでは区別されます。plist ファイルを参照するすべての場所で、大文字と小文字が正しく使用されていることを確認してください。

または、シミュレーターではアプリケーション バンドルに直接書き込むことができますが、デバイスではこれは不可能であり、アプリケーションのサンドボックス内の特定のディレクトリ (通常はドキュメント ディレクトリ) にのみ書き込むことができます。通常、最初の実行時に plist をドキュメント ディレクトリにコピーし、その後はそのファイルを使用します。

于 2012-08-26T06:05:53.237 に答える
1

私があなたのコードを正しく理解していれば、viewDidLoadでのみplistファイルをロードしますが、おそらくこの関数は、ビューを初めてロードしたときにのみ呼び出されます。それを機能させるには、plist を viewDidAppear にロードする必要があります。このようなもの:

- (void)viewDidAppear {
  NSBundle *bundle = [NSBundle mainBundle];
  NSURL *plistURL = [bundle URLForResource:@"CrossfitWOD" withExtension:@"plist"];
//put the contents of the plist into a NSDictionary, and then into names instance variable
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
self.names = dictionary;
//take all the keys in the dictionary and make an array out of those key names
self.keys = [self.names allKeys];

for (NSString *nameCheck in keys){
    self.details = [names valueForKey:nameCheck];
    if ([[self.details valueForKey:@"Type"] isEqualToString:@"The Benchmark Girls"]) {
        [buildBenchmarkGirls addObject:nameCheck];
    }else if ([[self.details valueForKey:@"Type"] isEqualToString:@"The New Girls"]) {
        [buildTheNewGirls addObject:nameCheck];
    }else {
        [buildHeroes addObject:nameCheck];
    }
}

NSString *filePath = [self dataFilePath];
NSMutableDictionary *savedWorkout = [[NSMutableDictionary         alloc]initWithContentsOfFile:filePath];
self.savedNames = savedWorkout;
self.savedDFGWorkouts = [[savedWorkout allKeys]     sortedArrayUsingSelector:@selector(compare:)];

 [self.tableView reloadData];

}
于 2012-08-27T15:30:22.210 に答える
0

シミュレーターでは動作し、電話では動作しない場合、ほぼ確実に問題はタイミングの問題です。実際の電話でファイルを保存すると、シミュレーターよりもはるかに時間がかかります。

次のことを行う必要があります。

  • ファイルを保存してログに記録し、保存からの戻りコードをログに記録します。保存方法でリターン コードが返されない場合は、NSFileManager を使用して、ファイルが実際にあるべき場所にあり、サイズも確認します。これには時間がかかりますが、実行する必要があります。

  • テーブルがこれとあれの数を要求している場合は、ログに記録し、返されたものをロットします。これは、ファイルが保存される前に行われることがあります。

時間と労力がかかりますが、関連するすべてのログを記録し始めると、それを見つけることができます。私は今日、決して起こらないと思っていた競合状態を追跡するのに 6 時間を費やしました。問題を確認できたのは、膨大な数のメッセージを調べた後でした。

いずれかのファイルが保存されていないか、思っていた場所にないか、電話のタイミングにより、シミュレーターで発生するイベントよりも後にイベントが発生することがほぼ確実にわかります。

于 2012-08-27T22:58:33.010 に答える