我正在开发一个程序,但在更新 tableView 时遇到问题。我几乎有一个 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 开发的苹果开发人员指南,但我仍然卡住了。