2012-03-16 47 views
2

时被保留,我有两个表视图控制器,EmployeeListViewControllerEmployeeGroupDetailViewController数据重新选择

EmployeeListViewController将变量_uniqueId发送给所选的每个表格单元格至EmployeeGroupDetailViewController,该表格用于查找有关所选Employee的详细信息。

的问题是,当我选择一个员工,一切都看起来很好。但是如果我回去(使用“后退”按钮)并选择一个不同的员工,详细视图将显示与以前相同的值。

我尝试过将一些代码viewDidLoad()viewWillAppear(),但我已经完成的是选择一个新员工时,详细视图显示来自先前选择员工的价值观,但如果我滚动,这样的细胞离开视野然后释放,信息被更新。

为什么不详细视图表中获取从一开始更新?

EmployeeListViewController.m

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Navigation logic may go here. Create and push another view controller. 
if (self.details == nil) { 

self.details = [[[EmployeeGroupDetailViewController alloc] initWithNibName:@"EmployeeGroupDetailViewController" bundle:nil] autorelease]; 
} 
// ... 
EmployeeInfo *info = [_EmployeeInfos objectAtIndex:indexPath.row]; 
_details.uniqueId = info.uniqueId; 
// Pass the selected object to the new view controller. 
[self.navigationController pushViewController:_details animated:YES]; 
//[EmployeeGroupDetailViewController release]; 

} 

EmployeeGroupDetailViewController.m

// 
// EmployeeGroupDetailViewController.m 
// Whowho 
// 
// Created by Carl Franzon on 2012-03-14. 
// Copyright (c) 2012 Atea SE. All rights reserved. 
// 

#import "EmployeeGroupDetailViewController.h" 
#import "EmployeeDB.h" 
#import "EmployeeDetails.h" 

@implementation EmployeeGroupDetailViewController 


@synthesize uniqueId = _uniqueId; 

@synthesize headerDict = _headerDict; 
//@synthesize dicContactInfo = _dicContactInfo; 
//@synthesize dicEmploymentInfo = _dicEmploymentInfo; 
@synthesize sortedKeys = _sortedKeys; 

@synthesize details = _details; 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    //Lookup entry in DB and set labels accordingly 
    //EmployeeDetails *details = [[EmployeeDB database] EmployeeDetails:_uniqueId]; 
    _details = [[EmployeeDB database] EmployeeDetails:_uniqueId]; 

    //if (details != nil) { 
     NSArray *arrTemp1 = [[NSArray alloc] initWithObjects:_details.email,_details.shortNo,_details.mobileNo,_details.directNo,nil]; 

     NSArray *arrTemp2 = [[NSArray alloc] initWithObjects:@"Bob",@"Bill",@"Bianca",nil]; 

     NSArray *arrTemp3 = [[NSArray alloc] initWithObjects:@"Candice",@"Clint",@"Chris",nil]; 

     NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:arrTemp1,@"A",arrTemp2,@"B",arrTemp3,@"C",nil]; 

     self.headerDict =temp; 

     [temp release]; 

     self.sortedKeys =[[self.headerDict allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

     [arrTemp1 release]; 

     [arrTemp2 release]; 

     [arrTemp3 release]; 

    // } 



    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
// self.employeeNoLabel = nil; 
// self.nameLabel = nil; 
// self.emailLabel = nil; 
// self.regionLabel = nil; 
// self.officeLabel = nil; 
// self.departmentLabel = nil; 
// self.locationLabel = nil; 
// self.roleLabel = nil; 
// self.activeLabel = nil; 
// self.workHoursLabel = nil; 
// self.managerLabel = nil; 
// self.costCentreLabel = nil; 
// self.adLabel = nil; 
// self.origCompNameLabel = nil; 
// self.shortNoLabel = nil; 
// self.directNoLabel = nil; 
// self.mobileLabel = nil; 
// self.sexLabel = nil; 
// self.shirtJacketLabel = nil; 
// self.pantsLabel = nil; 
// self.scgLabel = nil; 
// self.tempRoleLabel = nil; 



} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    //Lookup entry in DB and set labels accordingly 
    // EmployeeDetails *details2 = [[EmployeeDB database] EmployeeDetails:_uniqueId]; 

    _details = [[EmployeeDB database] EmployeeDetails:_uniqueId]; 


    //if (details != nil) { 
     NSArray *arrTemp1b = [[NSArray alloc] initWithObjects:_details.email,_details.shortNo,_details.mobileNo,_details.directNo,nil]; 

     NSArray *arrTemp2b = [[NSArray alloc] initWithObjects:@"Bob",@"Bill",@"Bianca",nil]; 

     NSArray *arrTemp3b = [[NSArray alloc] initWithObjects:@"Candice",@"Clint",@"Chris",nil]; 

     NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:arrTemp1b,@"A",arrTemp2b,@"B",arrTemp3b,@"C",nil]; 

     self.headerDict =temp; 

     [temp release]; 

     self.sortedKeys =[[self.headerDict allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

     [arrTemp1b release]; 

     [arrTemp2b release]; 

     [arrTemp3b release]; 
    //} 



} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return [self.sortedKeys count]; 
} 

- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section 
{ 
    return [self.sortedKeys objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//#warning Incomplete method implementation. 
    // Return the number of rows in the section. 

    NSArray *listData =[self.headerDict objectForKey: 
         [self.sortedKeys objectAtIndex:section]]; 
    return [listData count]; 

    //return 0; 
} 

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *listData =[self.headerDict objectForKey: 
         [self.sortedKeys objectAtIndex:[indexPath section]]]; 


    UITableViewCell * cell = [tableView 
           dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 

    if(cell == nil) { 

     //Initialize cell with left lable style 
     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    //cell.textLabel.text = @"default"; 
    cell.textLabel.text = [listData objectAtIndex:row]; 


// static NSString *CellIdentifier = @"Cell"; 
//  
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
// if (cell == nil) { 
//  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
// } 

    // Configure the cell... 

    return cell; 
} 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
    */ 



    NSArray *listData =[self.headerDict objectForKey: 
         [self.sortedKeys objectAtIndex:[indexPath section]]]; 
    NSUInteger row = [indexPath row]; 
    NSString *rowValue = [listData objectAtIndex:row]; 

    NSString *message = [[NSString alloc] initWithFormat:rowValue]; 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"You selected" 
          message:message delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [message release]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

- (void)dealloc 

{ 
// self.employeeNoLabel = nil; 
// self.nameLabel = nil; 
// self.emailLabel = nil; 
// self.regionLabel = nil; 
// self.officeLabel = nil; 
// self.departmentLabel = nil; 
// self.locationLabel = nil; 
// self.roleLabel = nil; 
// self.activeLabel = nil; 
// self.workHoursLabel = nil; 
// self.managerLabel = nil; 
// self.costCentreLabel = nil; 
// self.adLabel = nil; 
// self.origCompNameLabel = nil; 
// self.shortNoLabel = nil; 
// self.directNoLabel = nil; 
// self.mobileLabel = nil; 
// self.sexLabel = nil; 
// self.shirtJacketLabel = nil; 
// self.pantsLabel = nil; 
// self.scgLabel = nil; 
// self.tempRoleLabel = nil; 


    [_headerDict release]; 
    [_sortedKeys release]; 
    [super dealloc]; 
} 

@end 

而且,行每个部分的数量似乎并没有得到更新,每一个新的选择员工。

+0

直接访问你的实例变量是不是最好的想法。考虑在你需要访问值的地方使用你的属性而不是dealloc。 – 2012-03-16 18:31:35

+0

谢谢,我对ios开发相当陌生,这是我第一次尝试制作功能性应用程序。我会尽量在适用的情况下更改此项。 – carlfranzon 2012-03-17 09:42:21

回答

0

它使用时的主从模式用于主视图控制器创建的每个细节项目被选择时的详细视图控制器的新实例,该实例传递的所有权关闭的导航控制器是惯例。 (实际上,如果你的目标是iOS 5.0+并使用故事板,那么它就是以这种方式为你完成的,而你所做的只是告诉详细视图控制器使用哪个项目。)但是,这并不是直接导致你的问题的原因。

您的详细视图控制器更新其内部数据来匹配任何uniqueIDviewWillAppear:设置就可以了(viewDidLoad:,所以它发生,如果两次视图的被卸载)。但它没有任何地方能够更新表格来反映新数据。调用[self.tableView reloadData]应该这样做。

+0

是的,通过阅读其他一些问题,我发现我应该使用你提到的reloadData方法。但我在哪里放?我试图把它放在viewDidLoad和viewWillAppear中,但是当我把它放在那里时,应用程序会一直崩溃。 – carlfranzon 2012-03-17 09:36:30

+0

当新员工在第一部分中有不同数量的行时,产生的崩溃可能与计算错误的listData计数有关吗?我收到的错误陈述了有关索引范围越界或类似的情况。 – carlfranzon 2012-03-17 09:40:44