我正在研究一个程序,但我遇到了让tableView更新的问题。我几乎有一个tableView,起初只有空行。然后用户点击+号并在堆栈顶部推送新的表视图,用户可以从主列表中选择要添加到初始表的行。用户选择的行将添加到数组中。当用户单击后退按钮时,第一个表不会更新。但是,如果我按HOME键然后重新加载应用程序,则第一个表视图将显示更新的数组。我已经包含了来自两个表和我的gloabl数组的代码。
第一个表的.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 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
下面的是第二个表的.h
@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
最后是我使用方法
的全局数组@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 :(得分: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;
}
答案 1 :(得分:0)
将表格1.h更改为以下内容:
@class baseTableViewViewController; @class BaseInfoDataController;
@interface baseTableViewViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@end
我必须将它定义为UITableViewController而不是UIViewController。一旦我纠正了这个,它刷新了清单。