gpt4 book ai didi

iphone - 当数据对象符合 NSCoding 时,UITableView 不显示新条目

转载 作者:行者123 更新时间:2023-11-28 17:40:25 25 4
gpt4 key购买 nike

当我使对象类符合 NSCoding 时,tableView 不显示新条目。怀疑与对象初始化有关,但无法解决。可能非常基本但找不到错误。这是我简化后发布的代码:

自定义对象:

//  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 根 Controller - 省略了一些方法:

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

最佳答案

哦,伙计,我很尴尬......它与 NSCoding 没有直接关系,但是当我使数据类 NSCoding 兼容时,我在 viewDidLoad 中添加了:

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

第一行没问题,但第二行分配了一个新的指针地址给 self.list 并且 tempArray 后来被自动释放了......由于此行未在“不符合 NSCoding 标准”的版本中执行,因此它似乎在当时起作用,而不是在实现 encodeWithCoder 和 initWithCoder 时起作用。

因为这个 self.list 是 nil 而不是在 add: 方法中被初始化和准备好我添加了一个新实例。它创建得很好,但从未添加到数组中,因此从未显示在 tableView 中。

这很容易在 viewDidLoad 中使用而不是修复:

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

最糟糕的是,我因此而抓狂,并确信这是因为 NSCoding 和对象的初始化......

关于iphone - 当数据对象符合 NSCoding 时,UITableView 不显示新条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8369309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com