UITableViewController重置数据源

时间:2015-04-15 18:51:35

标签: uitableview

我有一个UITableViewController,它作为UIPopOverController的视图控制器呈现。 tvc有三个属性 - 一个字典和两个数组。该字典包含两个数组,用于填充其他两个属性。我遵循这个过程:

在根视图控制器中,我初始化表视图控制器 在触发器上,我设置表视图控制器的字典属性并调用方法(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]调用,它就会重置viewcontroller属性

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];

}

视图控制器是init中的另一个视图控制器(contentViewController)在视图中加载:

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

并在popover中呈现如下:

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 个答案:

答案 0 :(得分:1)

在以下代码块中显示popover:

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

在调用viewDidLoad之后,[self.mySavedSearchesPopover reloadData]方法正在触发,而reloadData正在使用空数组重新创建这些属性。出现弹出框后,您需要调用{{1}}。