2012-08-26 76 views
1

如果這是一個轉發,我很抱歉,但我一直在網上搜索,似乎無法找到任何有效的東西。我有一個鍛鍊列表,顯示在捆綁中的plist中收集的tableview中。還有一個單獨的選項卡,允許用戶構建自己的鍛鍊並將其保存在文檔文件夾plist文件中。保存後,它們將被添加到表格視圖中。在模擬器中,everyuhting正常工作。但在實際的設備上,除非我長時間關閉程序,從xcode重新加載程序或關閉手機,否則不會更新。我曾嘗試將[[self tableview] reload]添加到「viewDidLoad」,「viewWillappear」和「viewDidAppear」,它們都不起作用。再次保存文件,更新在模擬器中工作,並且不會立即在手機中更新。有什麼建議麼?謝謝。tableview將不會更新電話

編輯:我知道它是一個長一段代碼,而應該是直線前進(希望笑)

#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 

回答

1

如果我明白你的代碼,你只能在viewDidLoad中加載plist文件,但很可能只有當你第一次加載你的視圖時才調用這個函數。要使其工作,您應該在viewDidAppear中加載plist 。類似這樣的:

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

} 
+0

是的,這是問題=)。我只在viewDidLoad中加載了plist文件。在模擬器上,它非常爽快,但在電話上,它不是。將我所有的代碼移動到viewDidAppear,並且一切都是excelsior = D。非常感謝。 – TigerCoder

2

不同的行爲往往是與在文件名中使用不正確的大小寫有關 - 模擬器不區分大小寫,並且設備是。檢查您在任何引用plist文件的地方都使用了正確的案例。

或者,在模擬器上,您可以直接寫入應用程序包,但在設備上這是不可能的,您只能寫入應用程序沙箱中的某些目錄,通常是文檔目錄。您通常會在第一次運行時將plist複製到文檔目錄,然後使用該文件。

+0

我在代碼的頂部定義了案例,只是使用了變量名,所以這不應該是一個問題。 – TigerCoder

+0

那麼,你沒有在你的問題中包含任何代碼,所以我不得不猜測!請參閱修改其他建議。 – jrturton

+0

對不起。這是tableview的代碼。再次感謝 – TigerCoder

0

如果它在模擬器中工作並且不在電話上,幾乎可以肯定問題是時間問題。在真實手機上保存文件比在模擬器上花費的時間要長得多。

你應該做到以下幾點:

  • 當您保存文件,記錄它,並記錄從保存的返回碼。如果您保存的方式不提供返回碼,請使用NSFileManager來驗證該文件實際上應該位於哪個位置,甚至是它的大小。這需要時間來做,但你應該這樣做。

  • 當你的表是詢問這個和那個的數量,記錄它,並且返回什麼。您可能會發現這是在保存文件之前發生的。

這需要時間和精力,但如果你開始記錄所有相關的事情,你可以找到它。今天我只花了6個小時追蹤我原本以爲永遠不會發生的競賽狀況,而且只有在看到一大堆信息後才能看到問題。

幾乎可以肯定你會看到任何一個文件都沒有保存,它不在你認爲的位置,或者手機計時意味着某些事件發生的時間晚於模擬器。