2012-12-02 67 views
-1

我正在研究一個程序,並且遇到了要更新tableView的問題。幾乎我有一個tableView,並且最初只有空行。然後用戶點擊+符號,然後在堆棧頂部推入新的tableview,用戶可以從主列表中選擇要添加到初始表中的行。用戶選擇的行被添加到數組中。當用戶單擊後退按鈕時,第一個表不會更新。但是,如果按HOME鍵,然後重新加載應用程序,則第一個表視圖將顯示更新後的數組。我已經包含了來自表格和我的gloabl數組的代碼。對於第二個表(主列表)向數組添加新行後,TableView不會更新[[self tableView] reloadData]不起作用

@class BaseInfo; @interface MasterListTableViewController() 


@end 

@implementation MasterListTableViewController @synthesize delegate; 


- (id)initWithStyle:(UITableViewStyle)style { 
    self = [super initWithStyle:style]; 
    if (self) { 

    } 
    return self; } 

- (void)viewDidLoad { 
    [super viewDidLoad]; } 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. } 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [self.dataController countOfList]; } 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Base"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil){ 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 

    } 

    BaseInfo *infoAtIndex = [self.dataController objectInListAtIndex:indexPath.row]; 
    [[cell textLabel] setText:infoAtIndex.name]; 
    [[cell detailTextLabel] setText:infoAtIndex.icao]; 

    return cell; } 




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSMutableArray *addBaseList = [[NSMutableArray alloc]init]; 
    self.masterList = addBaseList; 
    BaseInfo *inform; 


    if ([selectedCell accessoryType] == UITableViewCellAccessoryNone){ 
     [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

     inform = [self.dataController objectInListAtIndex:indexPath.row]; 
     NSLog(@"%@",inform.name); 
     NSLog(@"%@",inform.icao); 

     [DataClass.getInstance addObject:inform]; 




    }else{ 
     [selectedCell setAccessoryType:UITableViewCellAccessoryNone]; 

    } 

    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    } 

-(void)awakeFromNib{ 
    [super awakeFromNib]; 
    self.dataController = [[MasterListDataController alloc]init]; } 

-(IBAction)cancel:(id)sender{ 

    [self masterListTableViewControllerDidCancel:self]; 

} 

-(IBAction)done:(id)sender{ 

    [self masterListTableViewControllerDidSave:self]; } 


-(void)masterListTableViewControllerDidCancel:(MasterListTableViewController 
*)controller{ 

    [self dismissViewControllerAnimated:YES completion:nil]; } 



-(void)masterListTableViewControllerDidSave:(MasterListTableViewController 
*)controller{ 

    [self dismissViewControllerAnimated:YES completion:nil]; } 


@end 
下面

爲第一表.m文件

@interface baseTableViewViewController() 

@end 

@implementation baseTableViewViewController 

//@synthesize tableView = _tableView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

    } 
    return self; } 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    } 

-(void)viewDidlAppear:(BOOL)animated{ 



} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. } 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [[[DataClass getInstance]allItems]count]; } 

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


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"base"]; 

    if(!cell){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"base"]; 
    } 

    BaseInfo *infoAtIndex = [[[DataClass getInstance] allItems] objectAtIndex:[indexPath row]]; 

    [[cell textLabel] setText:[infoAtIndex name]]; 
    [[cell detailTextLabel] setText:[infoAtIndex icao]]; 
    return cell; } 

-(void)awakeFromNib{ 
    [super awakeFromNib]; } 

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:YES]; 
    [[self tableView]reloadData]; 
    } 

@end 

.h file for first table 



@class baseTableViewViewController; @class BaseInfoDataController; 

@interface baseTableViewViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, retain) UITableView *tableView; 


@end 

.m文件是用於第二表

@class MasterListTableViewController; @class BaseInfo; 




@interface MasterListTableViewController : UITableViewController 

@property (nonatomic, assign) id delegate; @property (nonatomic, copy) NSMutableArray *masterList; @property (nonatomic, retain) NSArray 
*baseNames; @property (nonatomic, retain) NSArray *icao; @property (strong, nonatomic) MasterListDataController *dataController; @property (nonatomic, copy)BaseInfo *inform; 


- (IBAction)cancel:(id)sender; 
- (IBAction)done:(id)sender; 

-(void)masterListTableViewControllerDidCancel:(MasterListTableViewController 
*)controller; 
-(void)masterListTableViewControllerDidsave:(MasterListTableViewController 
*)controller; 

@end 
的.H

,最後是我用的方法

@implementation DataClass @synthesize userBaseList; 

+(DataClass *)getInstance{ 
    static DataClass *instance = nil; 
    //@synchronized(self) 
    { 
     //if(instance ==nil) 
     if(!instance) 
     { 
      //instance = [DataClass new]; 
      instance = [[super allocWithZone:nil]init]; 
     } 
    } 
    return instance; } 

-(void)addObject:(BaseInfo*)info{ 
    [self.userBaseList addObject:info]; 
    } 
-(void)removeObjectAtIndex:(NSInteger)atIndex{ 
    [self.userBaseList removeObjectAtIndex:atIndex]; } 

+(id)allocWithZone:(NSZone *)zone{ 
    return [self getInstance]; } 
-(id)init{ 
    self = [super init]; 
    if (self){ 
     userBaseList = [[NSMutableArray alloc]init]; 
    } 
    return self; } 
-(NSArray *)allItems{ 
    return userBaseList; } @end 

我認爲解決方案必須是非常簡單的東西,我錯過了,但我只是沒有看到它。我已閱讀了蘋果開發者指南,瞭解了uitableview的開發情況,但我仍然陷入困境。

+0

在「MasterListTableViewController」的DidSelectedRowAtIndexPath方法中,您將在DataClass中添加對象以反映「baseTableViewViewController」,我是否正確?你可以請張貼代碼嗎? –

+0

已發佈。我以爲我是第一次這樣做。它在第二張表.m編碼帖子下。是的,這是正確的,但是當我回到第一個表格時,行不會更新。但是,如果我按Home按鈕然後重新打開應用程序,數組將被加載並且新行在那裏。 – acithium

+0

我只是想通了。我必須將表1定義爲UITableViewController而不是UIViewController。我不知道爲什麼這不讓我的表更新,但它現在工作! – acithium

回答

0

改變表1.H以下幾點:

@class baseTableViewViewController; @class BaseInfoDataController; 

@interface baseTableViewViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, retain) UITableView *tableView; 


@end 

我不得不把它定義爲一個UITableViewController,而不是一個UIViewController。一旦我糾正了這一點,它刷新了列表。

0

您正在分配一個DataClass的超類的實例,而不是DataClass。你不需要定義allocWithZone,反正它是錯誤的。你只需要這樣:

-(id)init { 
    self = [super init]; 
    if (self) { 
     userBaseList = [[NSMutableArray alloc]init]; 
    } 
    return self; 
} 
+(DataClass *)getInstance { 
    static DataClass *instance = nil; 
    if(!instance) 
     instance = [[DataClass alloc]init]; 
    return instance; 
} 
+0

感謝您的幫助。改變了我的代碼,但我仍然遇到同樣的問題 – acithium