2012-12-02 139 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