0

我的 TableViewController 中的单元格是从 NSMutableArray 属性itemList填充的。

@interface MenuViewController : UITableViewController <JSONURLConnectionDelegate>

@property (nonatomic,strong) JSONURLConnection *jsonConnection;
@property (nonatomic,strong) NSURL *jsonURL;   
@property (nonatomic,strong) DataBase *db;
@property (nonatomic,strong) NSString *sel_category;
@property int categoryCount;   
@property (nonatomic,strong) NSMutableArray *itemList;    

@end

加载 TableViewController 时,会调用一个方法从我的 SQLite 数据库中提取列表。一旦该列表被拉出,它被解析并保存为 NSMutableArray 属性,TableView 数据被重新加载,从而动态填充单元格。

根据选择的单元格确定从 sqlite 数据库中提取的新列表,然后保存到目标控制器的 itemList 属性。

由于导航堆栈下的 segue 数量会有所不同并且取决于选择的单元格,因此我通过执行 self segue 并继承新的 NSDictionary 属性来重新加载相同的 TableViewController。

在此处输入图像描述

当我继续堆栈时,一切都很好。问题是当我点击后退按钮返回堆栈然后尝试再次返回时。当我将导航堆栈备份到先前加载的视图控制器时,问题似乎是不再可访问原始属性。有没有其他人遇到过这个问题并且知道解决方案?我猜这是因为我需要让我的 TableViewController 类的一个新实例更动态地向下传递堆栈,而我没有正确执行它。这是我的segue代码:

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSDictionary *item = [self.itemList objectAtIndex:indexPath.row];

    NSData *jsonData = [[item objectForKey:@"child_categories"] dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *child_categories = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

    self.categoryCount  = [child_categories count];
    self.sel_category   = [item objectForKey:@"id"];


    NSString *sql = [[NSString alloc]initWithFormat:@"WHERE parent_category_id = %@ ORDER BY name ASC", _sel_category];
    self.itemList = [self.db getItemsFrom:@"product_category" where:sql];

    if ([child_categories count] > 0)
    {
        [self performSegueWithIdentifier:@"segue1" sender:self];
    }
    else if([child_categories count] == 0)
    {
        [self performSegueWithIdentifier:@"segue2" sender:self];
    }
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segue1"])
    {
        MenuViewController *mvc = [segue destinationViewController];
        mvc.jsonConnection  = self.jsonConnection;
        mvc.db              = self.db;
        mvc.sel_category    = self.sel_category;
        mvc.categoryCount   = self.categoryCount;
        mvc.itemList        = self.itemList;
        NSLog(@"Segue1");

    }
    else if([segue.identifier isEqualToString:@"segue2"])
    {
        NSLog(@"Segue2");
    }
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    //ignore segue from cell since we we are calling manually in didSelectRowAtIndexPath
    return (sender == self);
}
4

1 回答 1

0

@property (nonatomic,strong) NSMutableArray *selectedItemList;

我需要添加第二个名为 selectedItemList 的属性。

@interface MenuViewController : UITableViewController <JSONURLConnectionDelegate>

@property (nonatomic,strong) JSONURLConnection *jsonConnection;
@property (nonatomic,strong) NSURL *jsonURL;   
@property (nonatomic,strong) DataBase *db;
@property (nonatomic,strong) NSString *sel_category;
@property int categoryCount;   
@property (nonatomic,strong) NSMutableArray *itemList;
@property (nonatomic,strong) NSMutableArray *selectedItemList;       

@end

selectedItemList是在选择单元格时创建的,而不是更改当前 itemList 属性。

NSString *sql = [[NSString alloc]initWithFormat:@"WHERE parent_category_id = %@ ORDER BY name ASC", _sel_category];
self.selectedItemList = [self.db getItemsFrom:@"product_category" where:sql];

而是将该属性传递给新的 TableViewController。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segue1"])
    {
        MenuViewController *mvc = [segue destinationViewController];
        mvc.jsonConnection  = self.jsonConnection;
        mvc.db              = self.db;
        mvc.sel_category    = self.sel_category;
        mvc.categoryCount   = self.categoryCount;
        mvc.itemList        = self.selectedItemList;
        NSLog(@"Segue1");

    }
    else if([segue.identifier isEqualToString:@"segue2"])
    {
        NSLog(@"Segue2");
    }
}
于 2013-07-02T17:38:08.337 回答