当我试图在iphone上启动应用程序时,它不会显示TableView

时间:2012-05-20 09:54:42

标签: iphone objective-c xcode uitableview

所以我有一个应用程序在标签栏中有一个显示我的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。我确实删除了很多代码行,以便更容易阅读(删除搜索方法和选择行方法)。

请帮助我!

2 个答案:

答案 0 :(得分: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不是,我怀疑这是您的问题

答案 1 :(得分:0)

我认为问题在于 -

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

你需要使用 -

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