2013-07-02 65 views
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屬性。

由於導航堆棧下的segges數量不同,並且取決於所選單元格的大小,我通過執行self segue並載入新的NSDictionary屬性來重新加載相同的TableViewController。當我Segue公司上下堆疊

enter image description here

一切都很正常。問題是當我點擊返回按鈕返回堆棧,然後嘗試再次回落。當我將導航堆棧備份到先前加載的視圖控制器時,問題似乎與原始屬性不再可訪問。有沒有其他人有這個問題,並知道一個解決方案?我猜測這是因爲我需要一個新的我的TableViewController類的實例更動態地傳遞到堆棧,我沒有正確地做。這裏是我的代碼賽格瑞:

#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

當你回到堆棧時,你是否簡單地解僱了VC? – ApolloSoftware

+0

我只是按下了默認的後退按鈕。 – DJSK

+0

理論上你可以調用另一個segue來返回堆棧,並像你一直在做的那樣通過prepareForSegue傳遞你的屬性。但它可能會有點馬虎:) – ApolloSoftware

回答

0

@屬性(非原子,強)的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"); 
    } 
} 
相關問題