-1

プログラムに取り組んでいますが、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

2番目のテーブルの.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

以下は2番目のテーブルの.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開発のためのアップルデベロッパガイドを読みましたが、まだ行き詰まっています。

4

2 に答える 2

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;
}
于 2012-12-02T14:25:50.687 に答える
0

表1.hを次のように変更しました。

@class baseTableViewViewController; @class BaseInfoDataController;

@interface baseTableViewViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;


@end

UIViewControllerではなくUITableViewControllerとして定義する必要がありました。これを修正すると、リストが更新されました。

于 2012-12-02T15:06:08.960 に答える