2015-04-15 11 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