2011-12-03 3 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; 
,536,

첫 번째 줄은 괜찮지 만 두 번째 줄에는 self.list에 새 포인터 주소가 할당되고 tempArray는 나중에 자동으로 재생됩니다 ... "NSCoding이 아닌"버전에서이 줄이 실행되지 않았으므로 encodeWithCoder 및 initWithCoder가 구현 된 경우가 아닙니다.

때문에이 self.list가 초기화되지 않고 nil 이었기 때문에 add : 메소드에서 새 인스턴스를 추가했습니다. 그것은 잘 만들었지 만, 배열에 추가 된 적이 따라서 결코 tableView에 표시되지 않았습니다.

쉽게있는 viewDidLoad에서 사용하는 대신 고정 :

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

최악의 부분은 내가 ...

이 때문에 내 머리를 당기고 때문에 NSCoding이었다와 개체의 초기화 확신이었다이다
관련 문제