2011-12-03 9 views
1

オブジェクトクラスをNSCodingに準拠させると、tableViewは新しいエントリを表示しません。それはオブジェクトの初期化と関係がありますが、解決できないと思われます。非常に基本的かもしれませんが、エラーを見つけることはできません。ここで私はポストに簡略化されたコードは次のとおりです。データオブジェクトがNSCodingに準拠している場合、UITableViewは新しいエントリを表示しません

カスタムオブジェクト:

// DataObject.h 
#import <Foundation/Foundation.h> 
@interface DataObject : NSObject { 
NSString *name; 
} 
@property (nonatomic, copy) NSString *name; 
@end 

// DataObject.m 
#import "DataObject.h" 
@implementation DataObject 
@synthesize name; 

- (void)encodeWithCoder:(NSCoder*)encoder { 
[encoder encodeObject:name forKey:@"name"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
self = [super init]; 
if (!self) return nil; 

name = [[decoder decodeObjectForKey:@"name"] retain]; 
return self; 
} 

これらは、テーブルビューのルート・コントローラです - いくつかのメソッド省略されている:

// RootViewController.h 
#import <UIKit/UIKit.h> 
@interface RootViewController : UITableViewController { 
NSMutableArray *list; 
} 
@property (nonatomic, retain) NSMutableArray *list; 

- (void)add:(id)sender; 
- (NSString *)dataFilePath; 

@end 

// RootViewController.m 
#import "RootViewController.h" 
#import "DataObject.h" 

@implementation RootViewController 
@synthesize list; 

- (void)add:(id)sender 
{ 
DataObject *newEntry = [[DataObject alloc] init]; 
newEntry.name = @"Willy"; 
[self.list addObject:newEntry]; 

[self.tableView reloadData]; 

// Scroll table view to last row 
if ([list count] > 1) { 
    NSUInteger index = list.count - 1; 
    NSIndexPath *lastRow = [NSIndexPath indexPathForRow:index inSection: 0]; 
    [self.tableView scrollToRowAtIndexPath: lastRow atScrollPosition:  UITableViewScrollPositionTop animated: YES]; 
} 

[newEntry release]; 

} 

- (NSString *)dataFilePath 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
return [documentsDirectory stringByAppendingPathComponent:@"datafile"]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 

self.title = @"Names"; 
self.list = [[NSMutableArray alloc] init]; 

NSMutableArray *tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]]; 
self.list = tempArray; 

UIApplication *app = [UIApplication sharedApplication]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationWillResignActive:) 
              name:UIApplicationWillResignActiveNotification 
              object:app]; 


// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
self.navigationItem.rightBarButtonItem = self.editButtonItem; 

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] 
           initWithTitle:@"Add" 
           style:UIBarButtonItemStylePlain 
           target:self 
           action:@selector(add:)]; 
self.navigationItem.leftBarButtonItem = addButton; 
[addButton release]; 

[self.tableView reloadData]; 

} 

- (void)applicationWillResignActive:(NSNotification *)notification; 
{ 
[NSKeyedArchiver archiveRootObject:self.list toFile:[self dataFilePath]]; 
} 


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

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

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

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

// Configure the cell. 
NSUInteger row = [indexPath row]; 
DataObject *oneName = [self.list objectAtIndex:row]; 
cell.textLabel.text = oneName.name; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source. 
     NSUInteger row = [indexPath row]; 
     [self.list removeObjectAtIndex:row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 

} 


@end 
+0

私は何が起きているのかを理解しようと努力し、カスタムクラスがNSCodingに準拠していないときに絞り込むことができました。インスタンスが作成され、UItableViewControllerのNSMutableArrayリストに追加されました。オブジェクトがNSCodingに従うと、新しいインスタンスが作成されますが、リストには追加されません。 rray。なぜか分からない。何か案は ? – Will

答えて

0

ああ、私はとても恥ずかしい思い男を...それは直接NSCodingとは関係がありませんでしたが、データクラスNSCodingを準拠させたとき、私はviewDidLoadで追加しました:

最初の行は問題ありませんが、2番目の行はself.listに新しいポインタアドレスが割り当てられ、tempArrayは後で自動解放されました... この行は「NSCodingではない」バージョンでは実行されなかったため、 encodeWithCoderとinitWithCoderが実装されたときではありません。

このため、このself.listは初期化されていない代わりにnilだったので、add:メソッドで新しいインスタンスを追加しました。それはうまく作成されましたが、配列に追加されたことがないので、tableViewには表示されませんでした。

これは簡単のviewDidLoadで代わりに使用して修正されました:

NSMutableArray *tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]]; 
[self.list addObjectsFromArray:tempArray]; 

最悪の部分は、私はこのために私の髪を引っ張ったし、それは...ので、NSCodingとオブジェクトの初期化の

た確信していたということです
関連する問題