2012-03-16 70 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