tableview不会在手机上更新

时间:2012-08-26 05:57:41

标签: iphone xcode tableview updating

如果这是一个重新发布,我道歉,但我一直在网上搜索,似乎无法找到任何有用的东西。我有一个在tableview中显示的训练列表,这些训练集合在束中的plists中。我还有一个单独的选项卡,允许用户构建自己的锻炼并将其保存在文档文件夹plist文件中。保存后,它们将添加到表格视图中。在模拟器中,everyuhting工作正常。但在实际设备上,除非我长时间关闭程序,从xcode重新加载程序或关闭手机,否则不会更新。我已经尝试将[[table tableview] reload]添加到“viewDidLoad”,“viewWillappear”和“viewDidAppear”,但它们都不起作用。再一次,文件被保存,更新在模拟器中工作,并且它不会立即在手机中更新。有什么建议?感谢。

编辑:我知道这是一段很长的代码,但应该是直截了当(希望大声笑)

#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

3 个答案:

答案 0 :(得分:2)

模拟器和设备之间的不同行为通常与文件名中使用的错误案例有关 - 模拟器不区分大小写,而设备是。检查在引用plist文件的任何位置都使用了正确的大小写。

或者,在模拟器上,您可以直接写入应用程序包,但在设备上这是不可能的,您只能写入应用程序沙箱中的某些目录,通常是文档目录。您通常会在首次运行时将plist复制到文档目录,然后再使用该文件。

答案 1 :(得分: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];

}

答案 2 :(得分:0)

如果它在模拟器中工作但不在手机上,几乎可以肯定问题是时间问题。在真实手机上保存文件比在模拟器上保存的时间要长得多。

您应该执行以下操作:

  • 保存文件时,将其记录下来,并从保存中记录返回代码。如果您保存的方式不提供返回代码,请使用NSFileManager验证文件实际上应该在哪里,甚至是它的大小。这需要时间,但你应该这样做。

  • 当你的表要求的数量和那个,记录它,并批次返回什么。您可能会发现这是在文件保存之前发生的。

这需要时间和精力,但如果你开始记录所有相关的东西,你就可以找到它。今天我只花了6个小时来追踪我认为永远不会发生的竞争状况,而且只是在查看了大量的消息之后我才能看到问题。

几乎可以肯定,您会看到任何一个文件都没有保存,它不在您认为的位置,或者手机计时意味着某些事件发生的时间晚于模拟器中的事件。