0

したがって、TableViewを表示するタブバーにタブがあるアプリケーションがあります。最近変更し、テーブルのパスをデフォルトから変更しました
NSString *path = [[NSBundle mainBundle]pathForResource:@"Food" ofType"plist"];

これに置き換えます:
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"food.plist"];

だから私の問題は、XCodeのシミュレーターで起動しようとするとかなりうまくいくのですが、iPhoneで起動しようとすると何もTableView表示されず、検索バーと上部のナビゲーションバーが空になります。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"food.plist"];
    listOfItems = [[NSMutableArray alloc]initWithContentsOfFile:path];
    searchListOfItems = [[NSMutableArray alloc]init];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
    searchBar.barStyle = UIBarStyleBlackTranslucent;
    searchBar.showsCancelButton = NO;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    searchBar.delegate = self; 
    [[self tableView] setTableHeaderView:searchBar];

    searching = NO;
    letUserSelectRow = YES;
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addProduct:)];
    self.navigationItem.rightBarButtonItem = addButton;

    [self.tableView reloadData]; 
}

- (void)hideModalViewController:(NSNotification *)notif {
    [self dismissModalViewControllerAnimated:YES];
    [self viewDidLoad];
}
- (void)addProduct:(UIBarButtonItem *)button {
    BIDAddProductViewController *addProductVC = [[BIDAddProductViewController alloc]init];
    [self presentModalViewController:addProductVC animated:YES];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideModalViewController:) name:@"HideModalViewController" object:addProductVC];
}
- (void)viewDidUnload {
    [super viewDidUnload];
    self.childController = nil;
    self.tableView = nil;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"Delete");
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"food.plist"];
        NSMutableArray *listOfItemsToDelete = [[NSMutableArray alloc]initWithContentsOfFile:path];

        [[[listOfItemsToDelete objectAtIndex:indexPath.section]objectForKey:@"Products"] removeObjectAtIndex:indexPath.row];
        [listOfItemsToDelete writeToFile:path atomically:YES];
        [self viewDidLoad];
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SectionsTableIdentifier];
    }

    if(searching)
        cell.textLabel.text = [[searchListOfItems objectAtIndex:indexPath.row]valueForKey:@"ProductName"];
    else {
        NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
         NSArray *array = [[dictionary objectForKey:@"Products"]valueForKeyPath:@"ProductName"];
        NSString *cellValue = [array objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (searching)
        return 1;
    else
        return [listOfItems count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (searching)
        return [searchListOfItems count];
    else {
        NSDictionary *dictionary = [listOfItems objectAtIndex:section];
        NSArray *array = [dictionary objectForKey:@"Products"];
        return [array count];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(searching)
        return @"";
   return [[listOfItems objectAtIndex:section]valueForKey:@"SectionName"];
}

@end

プロジェクトとターゲットの設定でいくつかのことをしたことを述べたいと思いますが、その問題の前はすべてがうまくいっていました。それが問題なら私はiPhone4sを持っています。そして、読みやすくするためだけにコード行をたくさんカットしました(検索メソッドと行メソッドの選択を削除しました)。

私を助けてください!

4

2 に答える 2

0

問題は-にあると思います

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"food.plist"];

使用する必要があります-

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"food.plist"];
于 2012-05-20T12:16:42.807 に答える
0

あなたの問題はそれかもしれないと思います

NSString *path = [[NSBundle mainBundle]pathForResource:@"Food" ofType"plist"];

「Food.plist」を検索します(Fに注意してください)

とで

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingString:@"food.plist"];

「food.plist」を検索します(ローキャップfに注意してください)

お使いのデバイスは大文字と小文字を区別し、Macはデフォルトでは大文字と小文字を区別しないため、これが問題であると思われます

于 2012-05-20T10:28:53.713 に答える