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

我想提一下,我使用 Project 和 Target 设置做了一些事情,但是,在那之前,一切都很好。我有一个 iPhone 4s 如果那很重要。而且我确实削减了很多代码行,只是为了更容易阅读(删除了搜索方法和选择行方法)。

请帮我!

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 回答