2011-12-03 95 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; 
} 

這些是的TableView根控制器 - 省略的幾種方法:

// 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:

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

第一行是好的,但第二行分配了一個新的指針地址self.list和tempArray後來autoreleased ... 由於這條線沒有在「不符合NSCoding」版本執行它似乎工作,然後和而不是在執行encodeWithCoder和initWithCoder時。

由於這self.list是零而不是被初始化和準備好時,在添加:方法我添加了一個新的實例。它創建得很好,但從未添加到數組中,因此從未在tableView中顯示。

這使用而不是viewDidLoad中很容易固定:

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

最糟糕的是,我是拉正因爲如此我的頭髮,並相信這是因爲NSCoding和對象的初始化...