我的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);
}
答案 0 :(得分:0)
@property(非原子,强)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");
}
}