tableview将不会在手机上更新 [英] tableview will not update on phone

查看:55
本文介绍了tableview将不会在手机上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是转贴,我深表歉意,但我一直在搜寻网络,似乎找不到任何有效的方法.我有一个在列表视图中显示的锻炼列表,这些锻炼列表汇总在捆绑程序中.我还有一个单独的标签,该标签允许用户构建自己的锻炼并将其保存在documents文件夹的plist文件中.保存它们后,它们将被添加到表格视图中.在模拟器中,一切正常.但是在实际设备上,除非我长时间关闭程序,从xcode重新加载程序或关闭手机,否则不会更新该程序.我尝试将[[self tableview] reload]添加到"viewDidLoad","viewWillappear"和"viewDidAppear",但它们都不起作用.再次保存文件,更新在模拟器中起作用,并且不会立即在手机中更新.有什么建议?谢谢.

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.

我知道这是一段很长的代码,但是应该很简单(希望大声笑)

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

推荐答案

如果我正确理解您的代码,则仅在viewDidLoad中加载plist文件,但是很可能仅在您第一次加载视图时才调用此函数.为了使其正常工作,您应该加载plist 在viewDidAppear中.像这样:

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

}

这篇关于tableview将不会在手机上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆