-->

tableview will not update on phone

2020-04-20 21:24发布

问题:

If this is a repost, I apologize, but I have been scouring the net and cant seem to find anything that works. I have a list of workouts that I display in a tableview that are gathered in plists in the bundle. There is a also a separate tab that I have that allows a user to build their own workouts and save them in the documents folder plist file. Once they are saved, they are added to the table view. In the simulator, everyuhting works fine. But on the actual device, it is not updated unless I close the program for an extended period of time, reload the program from xcode, or turn the phone off. I have tried adding [[self tableview] reload] to "viewDidLoad", "viewWillappear", and "viewDidAppear" and none of them work. Once again, the file is saved, the updating does work in the simulator, and it doesn't update in the phone right away. Any suggestions? Thanks.

Edit: i know it is a long piece of code, but should be straight forward (hopefully lol)

#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:

If I understand right your code you load plist file only in viewDidLoad, but most likely this function called only when you first time load your view. To make it work you should load plist in viewDidAppear. Something like this:

- (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:

Different behaviour between the simulator and the device is often related to incorrect case being used in filenames - the simulator isn't case sensitive, and the device is. Check that you have the correct case used everywhere you reference the plist file.

Alternatively, on the simulator you are able to write directly to the application bundle, but on the device this is not possible and you can only write to certain directories in your application's sandbox, typically the documents directory. You would normally copy a plist to the documents directory on first run, and use that file thereafter.



回答3:

If it works in the Simulator and does not on the phone, almost for sure the problem is a timing issue. Saving files on a real phone takes much longer than on the simulator.

You should do the following:

  • when you save a file, log it, and log the return code from the save. If the way you save does not provide a return code, use NSFileManager to verify the file is in fact where it should be and even the size of it. This takes time to do but you should do it.

  • when your table is asking for the number of this and that, log it, and lot what is returned. You may find that that this comes before the files are saved.

It takes time and effort, but if you start logging all relevant things, you can find it. I just spend 6 hours today tracking down a race condition I had thought could never happen, and it was only after looking at a huge trail of messages that I was able to see the problem.

Almost for sure you will see that either file is not saved, its not where you thought it was, or that the phone timing means some events happen later than they do in the Simulator.