Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么UITableView的NSMutableArray数据在tableView:CellForRowatingIndexPath使用时丢失?_Ios_Objective C_Automatic Ref Counting - Fatal编程技术网

Ios 为什么UITableView的NSMutableArray数据在tableView:CellForRowatingIndexPath使用时丢失?

Ios 为什么UITableView的NSMutableArray数据在tableView:CellForRowatingIndexPath使用时丢失?,ios,objective-c,automatic-ref-counting,Ios,Objective C,Automatic Ref Counting,我有一个NSMutableArray*行在viewDidLoad中初始化并填充数据。显然,在这一点上,它有数据。在本例中,有三个条目 然后在tableView:cellforrowatinexpath中,我正在调用[rows-objectAtIndex:indexath.row]。但是,此时,行数组仍然包含三个条目,但这些条目的值是0x00000000,而不是原始值(例如,“id”是12345,但现在是0x00000000) 在我看来,行中的数据值在viewDidLoad和tableView:c

我有一个
NSMutableArray*行
viewDidLoad
中初始化并填充数据。显然,在这一点上,它有数据。在本例中,有三个条目

然后在
tableView:cellforrowatinexpath
中,我正在调用
[rows-objectAtIndex:indexath.row]
。但是,此时,
数组仍然包含三个条目,但这些条目的值是
0x00000000
,而不是原始值(例如,“id”是
12345
,但现在是
0x00000000

在我看来,
中的数据值在
viewDidLoad
tableView:cellforrowatinexpath
之间的某个位置被清空。这是什么原因造成的

编辑

代码如下:

ViewController.m

@implementation ViewController

NSMutableArray *rows;

- (void)viewDidLoad
{
    rows = [[NSMutableArray alloc] init];
    [rows setArray:myData]; // myData is als an NSMutableArray populated from JSON data.
}

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

{
    User *user = [rows objectAtIndex:indexPath.row]; // At this point 'rows' contains three entries but the values are empty.
}

@end
编辑2

以下是几项建议更改后的代码:

ViewController.m


0x000000000为零。这意味着该行没有指向任何地方

我在iOS 5.1上运行应用程序时也遇到同样的问题。问题是您假设viewDidLoad总是在tableview:CellForRowatineXpath.之前调用,但不一定是这样。第一个是视图控制器的方法。第二个是表视图数据源的方法。两者都是同一对象的事实是acci凹痕

在我的例子中,表视图控制器方法在viewDidLoad之前调用,然后在viewDidLoad之后再次调用,这会导致对视图表属性的某些修改总是进行重新加载


例如,尝试在–numberOfSectionsInTableView中初始化:

Uh,为什么setArray与只分配数组相比?
rows=myData;
。不需要alloc/init。
rows=[[NSMutableArray alloc]initWithArray:myData];
(你真的应该在界面中声明行,而不是在实现中。不太清楚ARC是如何处理它的。)@Darrell:hot-licks说:
@property(strong)NSMutableArray*rows;
在您的ViewController中。hDon不使用setArray。只需分配或使用initWithArray或arrayWithArray即可。这样做了。谢谢!有人知道为什么建议关闭此问题吗?这不是一个有价值的提交吗?@Darrell不客气。我不知道-2的原因以及关闭建议。Th这个问题很有价值,因为下一个家伙会发现这个问题。我确实花了很多时间来发现这个错误,我很高兴我现在可以帮助你。你的问题将帮助很多开发人员。我将投票支持它。
@interface ViewController()
{
    NSMutableArray *rows;
}

@implementation ViewController
- (void)setRowsFromJSON
{
    NSString *fileContents = [NSString stringWithContentsOfFile:@"data.json" encoding:NSUTF8StringEncoding error:nil];
    NSData *jsonData = [fileContents dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

    rows = [NSMutableArray arrayWithCapacity:[jsonArray count]];
    User *user;

    for (NSDictionary *aUser in jsonArray) {
        user = [[User alloc] init];
        user.id = [aUser valueForKey:@"id"];
        user.name = [aUser valueForKey:@"name"];
        [rows addObject:user];
    }
}

- (void)viewDidLoad
{
    [self setRowsFromJSON];
}

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

{
    User *user = [rows objectAtIndex:indexPath.row]; // At this point 'rows' contains three entries but the values are empty.
}

@end