2015-04-15 13 views
0

我有一個UITableViewController作爲UIPopOverController的視圖控制器呈現。 tvc有三個屬性 - 一個字典和兩個數組。該字典包含兩個數組,用於填充另外兩個屬性。我遵循這個過程:UITableViewController重置數據源

在根視圖控制器我初始化表視圖控制器 在觸發器上,我設置表視圖控制器的字典屬性,並調用方法(reloadData)。

- (void) reloadData { 

    self.arrGroupSearches = [self.dictSavedSearches objectForKey:@"Group Searches"]; 
    self.arrCustomerSearches = [self.dictSavedSearches objectForKey:@"Customer Searches"]; 

    [self.tableView reloadData]; 

} 

當我按照我所預期的方式逐步完成數據導入時。

self SavedSearchesPopoverViewController * 0x786653c0 0x786653c0 
UITableViewController UITableViewController  
_dictSavedSearches NSDictionary * 2 key/value pairs 0x79fb6ab0 
_arrGroupSearches NSArray * @"1 object" 0x79fb6b00 
_arrCustomerSearches NSArray * @"0 objects" 0x79faf410 

但一旦代碼命中[self.tableView reloadData]調用,它重置視圖 - 控制性能

self SavedSearchesPopoverViewController * 0x786653c0 0x786653c0 
UITableViewController UITableViewController  
_dictSavedSearches NSDictionary * 0 key/value pairs 0x79938560 
_arrGroupSearches NSArray * @"0 objects" 0x78744160 
_arrCustomerSearches NSArray * @"0 objects" 0x78744160 

我真的不知道我在做什麼錯在這裏。

編輯:這是整個執行:

@implementation SavedSearchesPopoverViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.arrCustomerSearches = [[NSArray alloc] init]; 
    self.arrGroupSearches = [[NSArray alloc] init]; 

    self.dictSavedSearches = [[NSDictionary alloc] init]; 

    self.tableView.rowHeight = 30.0; 
} 

- (void) reloadData { 

    self.arrGroupSearches = [self.dictSavedSearches objectForKey:@"Group Searches"]; 
    self.arrCustomerSearches = [self.dictSavedSearches objectForKey:@"Customer Searches"]; 

    [self.tableView reloadData]; 

} 



#pragma mark - Table view data source 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    UIView* vHeader = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.frame.size.width, 40.0)]; 

    if (section == 0) { 

     UILabel* lblPopoverTitle = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 5.0, 240.0, 30.0)]; 
     lblPopoverTitle.text = @"Group Saved Searches"; 
     lblPopoverTitle.textColor = GMT_BRIGHT_BLUE; 
     lblPopoverTitle.font = FORM_NAME; 
     lblPopoverTitle.textAlignment = NSTextAlignmentCenter; 
     [vHeader addSubview:lblPopoverTitle]; 

    } else if (section == 1) { 

     UILabel* lblPopoverTitle = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 5.0, 240.0, 30.0)]; 
     lblPopoverTitle.text = @"Customer Saved Searches"; 
     lblPopoverTitle.textColor = GMT_BRIGHT_BLUE; 
     lblPopoverTitle.font = FORM_NAME; 
     lblPopoverTitle.textAlignment = NSTextAlignmentCenter; 
     [vHeader addSubview:lblPopoverTitle]; 

    } 

    vHeader.backgroundColor = GMT_DARK_BLUE; 
    return vHeader; 

} 


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

    return 40.0; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    if (section == 0) { 

     return self.arrGroupSearches.count; 

    } else if (section == 1) { 

     return self.arrCustomerSearches.count; 

    } 

    return 0; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    NSString* strValue; 
    if (indexPath.section == 0) { 
     strValue = [self.arrGroupSearches objectAtIndex:indexPath.row]; 
    } else if (indexPath.section == 1) { 
     strValue = [self.arrCustomerSearches objectAtIndex:indexPath.row]; 
    } 

    cell.textLabel.text = strValue; 
    cell.textLabel.textColor = GMT_DARK_BLUE; 
    cell.textLabel.font = TABLE_CELL_FONT; 

    return cell; 
} 

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSDictionary* dictUserData = [[NSDictionary alloc] initWithObjectsAndKeys: 

            nil]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"cvc" object:self userInfo:dictUserData]; 

} 

視圖控制器在另外一個視圖控制器(contentViewController)初始化做負載:

self.mySavedSearchesPopover = [[SavedSearchesPopoverViewController alloc] init]; 

,並在酥料餅像這樣呈現:

if (showSavedSearches) { 

     //check to see if there are saved searches first before presenting popover 
     NSDictionary* dictSavedSearches = [self.myController readPlistFile:@"savedSearches"]; 

     if (![dictSavedSearches objectForKey:@"Error Message"]) { 

      self.mySavedSearchesPopover.dictSavedSearches = [self.myController getSavedSearches:dictSavedSearches]; 
      [self.mySavedSearchesPopover reloadData]; 
      [self.mySavedSearchesPopover.tableView reloadData]; 
      [self presentPopover:SAVED_SEACHES_POPOVER]; 

     } else { 

      UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"savedSearchErrorTitle", nil) message:NSLocalizedString(@"savedSearchErrorMsg", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 

    } 

- (void) presentPopover : (int) popoverID { 

    if (self.currentPopoverController != nil) { 
     [self.currentPopoverController dismissPopoverAnimated:YES]; 
     self.currentPopoverController = nil; 
    } 

    CGRect launchFrame; 

    ... 

    } else if (popoverID == SAVED_SEACHES_POPOVER) { 

     //this is inited in the update view method 
     launchFrame = CGRectMake(0.0, 70.0, 0.0, 180.0); 
     self.currentPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.mySavedSearchesPopover]; 
     self.currentPopoverController.popoverContentSize = CGSizeMake(240.0, 300.0); 

... 

//display popovercontroller 
    [self.currentPopoverController presentPopoverFromRect:launchFrame inView:self.view 
          permittedArrowDirections:UIPopoverArrowDirectionLeft 
              animated:YES]; 

回答

1

在下面的代碼塊中,您提供p opover:

[self.mySavedSearchesPopover reloadData]; 
[self.mySavedSearchesPopover.tableView reloadData]; 
[self presentPopover:SAVED_SEACHES_POPOVER]; 

調用[self.mySavedSearchesPopover reloadData]這是重建與空數組這些特性後,viewDidLoad方法燒製。出示彈出窗口後,您需要撥打reloadData

+0

感謝您的評論。我正在設置強大的屬性。我編輯了OP來包含額外的代碼。 – PruitIgoe

+0

其實劃傷。林編輯我的答案,以顯示我認爲問題是什麼。 – Erik

+0

問題出在我的viewDidLoad中,我是初始化數組和字典。一旦我停止這樣做,它的工作。 – PruitIgoe