iOS无法从NSMutableArray加载自定义对象

时间:2013-12-10 23:01:03

标签: ios nsarray

我有一个自定义对象Person,它有几个简单的属性。这些是姓名,用户名,电子邮件。

我从Web服务中提取JSON数据,该数据基本上是用户的NSArray。

然后我将其传递给自定义方法:

[self gatherPeopleFromArray:feedData];

这是该功能:

- (void) gatherTweetsFromArray:(NSArray *)data {

    if (data != nil) {

        NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:data.count];

        for (int i = 0; i < data.count; i++) {

            NSString *name = [[data objectAtIndex:i] objectForKey:@"name"]
            NSString *username = [[data objectAtIndex:i] objectForKey:@"username"]
            NSString *email = [[data objectAtIndex:i] objectForKey:@"email"]

            Person *aPerson = [Person alloc] init];

            aPerson.name = name;
            aPerson.username = username;
            aPerson.email = email;

            [temp insertObject:aPerson atIndex:i];

        }

        self.users = [NSMutableArray arrayWithArray:temp];

        // This is a test function example
        NSLog(@"%@", [self.users objectAtIndex:10]);            

    }

这很有效,并且所有内容都会按预期显示在控制台中,因此数组会填充自定义Person对象。

当我尝试在cellForRowAtIndexPath中访问它时,它会显示为null,就好像对象不能正确存在一样。

我确信这与将NSArray转换为NSMutableArrays等有关,但不确定我做错了什么。

任何帮助都会很棒,谢谢!

编辑:

这是我的cellForRowAtIndexPath:

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

    static NSString *identifier = @"user_cell";

    UserCell *cell = [self.usersTableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {
        cell = [[UserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }


    if (self.users != nil) {

        Person *thePerson = [self.users objectAtIndex:indexPath.row];

        NSLog(@"%@", thePerson.username);

        // Perform Cell Setup Here

    }


    return cell;

}

这是项目的开始,所以我不关心设置单元格,只关注数据模型。

0 个答案:

没有答案