iphone - NSMutableArray 在 reloadData 后变为 nil

标签 iphone objective-c cocoa-touch uitableview nsmutablearray

在我正在开发的 iPhone 应用程序中,我从 Web 服务获取一组数据,将其存储在可变数组中以在表格中显示,并在没有网络时将其存储在核心数据中作为备份。

我的数组在 application:didFinishLaunchingWithOptions: 中初始化,如下
tableArray = [[NSMutableArray alloc] initWithCapacity:100];
然后在调用 viewDidLoad 后填充数组,这意味着 UITableView 存在(通过检查调试器验证),并且在调用 reloadData 之前,我让 NSLog 打印出数组的内容,并且果然一切都在那里。

我的问题是,在调用 reloadData 之后,tableArray 变为 null,如 tableView:numberOfRowsInSection: 中的 NSLog 所示。

我完全不知道为什么会发生这种情况,尽管我只在 Cocoa 中编程了几个月,并且很容易错过一些明显的东西。

编辑:使用基本代码更新。

@interface MyAppDelegate : NSObject {<br/> ...<br/> NSMutableArray *tableArray;<br/> }<br/> @property (nonatomic, retain) NSMutableArray *tableArray;<br/> ...<br/> @end

@implementation MyAppDelegate<br/> @synthesize tableArray

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    tableArray = [[NSMutableArray alloc] initWithCapacity:100];

    masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:[NSBundle mainBundle]];
    [masterViewController.tableView setDelegate:self];
    [masterViewController.tableView setDataSource:self];
    ...
}

然后在masterViewController的viewDidLoad中调用一个名为runWithNetwork的方法:

- (void)runWithNetwork {
    ...
    NSArray *backpackArray = [[mainDict objectForKey:@"items"] objectForKey:@"item"];
    NSLog(@"%i", [backpackArray count]);
    for (int a=0; a<[backpackArray count]; a++) {
    NSDictionary *itemDict = [backpackArray objectAtIndex:a];
        NSString *ID = [[itemDict valueForKey:@"defindex"] stringValue];
        NSString *level = [[itemDict valueForKey:@"level"] stringValue];
        NSString *quality = [[itemDict valueForKey:@"quality"] stringValue];
        NSString *inventory = [[itemDict valueForKey:@"inventory"] stringValue];
        NSNumber *uid = [NSNumber numberWithInt:a];

        //Insert new
        myItem = [NSEntityDescription insertNewObjectForEntityForName:@"Backpack" inManagedObjectContext:managedObjectContext_];
        [myItem setValue:level forKey:@"level"];
        [myItem setValue:inventory forKey:@"inventory"];
        [myItem setValue:quality forKey:@"quality"];
        [myItem setValue:uid forKey:@"uid"];

        //Link to item db
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", ID];
        [fetch setEntity:[NSEntityDescription entityForName:@"GlobalItems" inManagedObjectContext:managedObjectContext_]];
        [fetch setPredicate:predicate];

        item = [[managedObjectContext_ executeFetchRequest:fetch error:nil] objectAtIndex:0];
        [myItem setValue:item forKey:@"item"];

        [tableArray addObject:[item valueForKey:@"name"]]; //a series of nsstrings

    }
    NSLog(@"%@, %i", tableArray, [tableArray retainCount]); // all strings are printed correctly here, retaincount is 1

    [masterViewController.tableView reloadData];
}

然后我转到我的表方法:

- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {  
    NSLog(@"%@, %i", tableArray, [tableArray retainCount]);  //(null) printed, 0 for retaincount
    return [tableArray count];  //returns 0
}

tableView位于MasterViewController的xib中,并且在那里正确链接。它仅在 MasterViewController 中描述并设置为 IBOutlet。 tableArray 仅在 MyAppDelegate.h 中描述,不会在其他地方重新创建。

最佳答案

正如我在评论中所说,很难弄清楚到底出了什么问题。但根据我帮助邮件列表等人员的经验,出现此类错误的最常见原因是当您认为自己拥有一个对象时却拥有两个不同的对象。例如,一个实例是在 nib 中创建的,另一个实例是在其他地方的代码中手动创建的。也许值得检查一下。

关于iphone - NSMutableArray 在 reloadData 后变为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3233085/

相关文章:

iphone - UI窗口动画

iphone - Objective-C/XCode : What class instantiation am I describing?

iphone - UIButton 作为 UITextfield 的 subview

objective-c - NSStream : Is there any airtight defense against blocking?

iphone - 有没有办法在未连接到调试器时捕获 iPhone 上 NSLog 的输出?

iphone - 从没有框架的类添加 UIView

iphone - 原力风景 iOS 6

iphone - 如何在三个 20 年代的 TableViewController 中获取用户定义的背景颜色

ios - 从没有时间戳和文件扩展名的 url 获取文件名 - Objective C

iphone - Core Data 存在于 iPhone 上吗?或者您将如何在 iPhone 上保存数据?