无法在cellForRowAtIndexPath上显示自定义类的NSArray:

时间:2014-12-11 05:14:46

标签: ios objective-c uitableview nsarray

所有,我失去了理智。我有一个类,下面的代码传递了一个NSArray" Events"到我的View Controller:

+ (NSArray *)eventsFromJSON:(NSData *)objectNotation error:(NSError **)error {
    NSError *localError = nil;
    NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

    if (localError != nil) {
        *error = localError;
        return nil;
    }

    NSMutableArray *events = [[NSMutableArray alloc] init];

    //NSArray *results = [parsedObject valueForKey:@"results"];
    NSLog(@"Count %lu", (unsigned long)parsedObject.count);

    for (NSDictionary *eventDic in parsedObject) {
        Event *event = [[Event alloc] init];

        for (NSString *key in eventDic) {
            if ([event respondsToSelector:NSSelectorFromString(key)]) {
                [event setValue:[eventDic valueForKey:key] forKey:key];
            }
        }

        [events addObject:event];

    }
    return events;
}

在我的视图控制器上,我有以下代码:

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

    static NSString *CellIdentifier = @"eventCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    Event *event = [self.eventData objectAtIndex:indexPath.row];
    cell.textLabel.text = event.title;
    cell.detailTextLabel.text = event.link;
    return cell;
}

- (void) didReceiveEvents:(NSArray *)events {
    self.eventData = [events copy];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

JSON我解析的示例如下:



[
    {
        "ID": 576,
        "title": “Event Title“,
        "status": "publish",
        "type": "tribe_events",
        "author": {
            "ID": 1,
            "username": "admin",
            "name": "admin",
            "first_name": “Name”,
            "last_name": “Last”,
            "nickname": "admin",
            "slug": "admin",
            "URL": "",
            "avatar": "1.gravatar.com/avatar/",
            "description": "",
            "registered": "-001-11-30T00:00:00+00:00",
            "meta": {
                "links": {
                    "self": "url.com/wp-json/users/1",
                    "archives": "url.com/wp-json/users/1/posts"
                }
            }
        },
        "content": "<p>Join us if you are interested.</p>\n",
        "parent": {
            "ID": 575,
            "title": “Title”,
            "status": "publish",
            "type": "tribe_events",
            "author": {
                "ID": 1,
                "username": "admin",
                "name": "admin",
                "first_name": “First”,
                "last_name": “Last”,
                "nickname": "admin",
                "slug": "admin",
                "URL": "",
                "avatar": "1.gravatar.com/avatar/?s=96",
                "description": "",
                "registered": "-001-11-30T00:00:00+00:00",
                "meta": {
                    "links": {
                        "self": "url.com/wp-json/users/1",
                        "archives": "url.com/wp-json/users/1/posts"
                    }
                }
            },
            "content": "<p>Join us if you are interested in joining </p>\n",
            "parent": 0,
            "link": "url.com/calendar/worship-service-practice/2014-12-06/",
            "date": "2014-12-04T20:09:21-06:00",
            "modified": "2014-12-04T20:09:21-06:00",
            "format": "standard",
            "slug": "worship-service-practice",
            "guid": "url.com/?post_type=tribe_events&#038;p=575",
            "excerpt": "<p>Join us if you are interested i</p>\n",
            "menu_order": 0,
            "comment_status": "closed",
            "ping_status": "closed",
            "sticky": false,
            "date_tz": "America/Chicago",
            "date_gmt": "2014-12-05T02:09:21+00:00",
            "modified_tz": "America/Chicago",
            "modified_gmt": "2014-12-05T02:09:21+00:00",
            "meta": {
                "links": {
                    "self": "url.com/wp-json/posts/575",
                    "author": "url.com/wp-json/users/1",
                    "collection": "url.com/wp-json/posts",
                    "replies": "url.com/wp-json/posts/575/comments",
                    "version-history": "url.com/wp-json/posts/575/revisions"
                }
            },
            "post_meta": {
                "EventStartDate": "2014-12-06 08:00:00",
                "EventEndDate": "2014-12-06 09:00:00"
            },
            "featured_image": null,
            "terms": {
                "tribe_events_cat": [
                    {
                        "ID": 13,
                        "name": "Community Outreach",
                        "slug": "community-outreach",
                        "description": "",
                        "parent": null,
                        "count": 52,
                        "link": "url.com/calendar/category/community-outreach/",
                        "meta": {
                            "links": {
                                "collection": "url.com/wp-json/taxonomies/tribe_events_cat/terms",
                                "self": "url.com/wp-json/taxonomies/tribe_events_cat/terms/9"
                            }
                        }
                    }
                ]
            }
        },
        "link": "url.com/calendar/worship-service-practice/2014-12-13/",
        "date": "2014-12-04T20:09:21-06:00",
        "modified": "2014-12-04T20:09:21-06:00",
        "format": "standard",
        "slug": "worship-service-practice-2014-12-13",
        "guid": "url.com/?post_type=tribe_events&#038;eventDate=2014-12-13#038;p=575",
        "excerpt": "<p>Join us if you are interested </p>\n",
        "menu_order": 0,
        "comment_status": "closed",
        "ping_status": "closed",
        "sticky": false,
        "date_tz": "America/Chicago",
        "date_gmt": "2014-12-05T02:09:21+00:00",
        "modified_tz": "America/Chicago",
        "modified_gmt": "2014-12-05T02:09:21+00:00",
        "meta": {
            "links": {
                "self": "url.com/wp-json/posts/576",
                "author": "url.com/wp-json/users/1",
                "collection": "url.com/wp-json/posts",
                "replies": "url.com/wp-json/posts/576/comments",
                "version-history": "url.com/wp-json/posts/576/revisions",
                "up": "url.com/wp-json/posts/575"
            }
        },
        "post_meta": {
            "EventStartDate": "2014-12-13 08:00:00",
            "EventEndDate": "2014-12-13 09:00:00"
        },
        "featured_image": null,
        "terms": {
            "tribe_events_cat": [
                {
                    "ID": 13,
                    "name": "Community Outreach",
                    "slug": "community-outreach",
                    "description": "",
                    "parent": null,
                    "count": 52,
                    "link": "url.com/calendar/category/community-outreach/",
                    "meta": {
                        "links": {
                            "collection": "url.com/wp-json/taxonomies/tribe_events_cat/terms",
                            "self": "url.com/wp-json/taxonomies/tribe_events_cat/terms/9"
                        }
                    }
                }
            ]
        }
    },
    {…
&#13;
&#13;
&#13;

我无法为我的生活获取Event类中的数据以显示在表格上。我有10个空行显示在表格上,但没有文字。当我将NSLog抛入cellForRowAtIndexPath函数并尝试打印event.title时,它会记录为null。关于我做错什么的任何想法?

2 个答案:

答案 0 :(得分:0)

您永远不会设置event.title。你遍历json的方式也是不正确的。如果它是属性,你应该这样设置:

for(NSDictionary *eventDic in [parsedObject objectForKey:@"results"]){
     Event *event = [[Event alloc] init];
     event.title = eventDic[@"title"]; //shorthand for [eventDic objectForKey:@"title"];
     event.link = eventDic[@"link"];
     [events addObject:event];
}

答案 1 :(得分:0)

事实证明我的代码很好,但是当我设置我的Event类时,我将属性设置为弱而强。一旦使属性属性变强,它就会保留所有指针,并且一切都很好。