知乎日报-ThemePage页_DailyThemesViewModel

包含文件

  • DailyThemesViewModel.h, DailyThemesViewModel.m

写在前面,数据原型,更多详见这里

示例数据网站为:http://news-at.zhihu.com/api/4/theme/13 由于数据有实时性,仅供参考

{
    "stories": [
    {
        "images": 
        [
            "http://pic2.zhimg.com/878b8960b617bfb4721a103e24a0509d.jpg"
        ],
        "type": ​0,
        "id": ​7785695,
        "title": "Necromanov:分裂红海:辐射4的喧嚣和争议"
    }, ...],
    "description": "如果你喜欢游戏,就从这里开始",
    "background": "http://p2.zhimg.com/55/e0/55e06f8fe322fd87b3261b204bae4786.jpg",
    "color": ​59647,
    "name": "开始游戏",
    "image": "http://p3.zhimg.com/64/5c/645cde143c9a371005f3f749366cffad.jpg",
    editors": [
    {
        "url": "http://www.zhihu.com/people/necromanov",
        "bio": "战略航空军旗舰的元帅",
        "id": ​3,
        "avatar": "http://pic4.zhimg.com/3553d57db_m.jpg",
        "name": "Necromanov"
    }, ...]
    "image_source": ""
}

1、模型定义

@interface DailyThemesViewModel : NSObject

@property (strong,nonatomic) NSMutableArray *stories;
@property (strong,nonatomic) NSString *imageURLStr;
@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSArray *editors;

- (void)getDailyThemesDataWithThemeID:(NSNumber *)themeID;

@end

2、更新数据

- (void)getDailyThemesDataWithThemeID:(NSNumber *)themeID {

    [HttpOperation getRequestWithURL:[NSString stringWithFormat:@"theme/%@",themeID] parameters:nil success:^(id responseObject) {
        NSDictionary *jsonDic = (NSDictionary *)responseObject;
        NSArray *storiesArr = jsonDic[@"stories"];
        NSMutableArray* tempArr = [NSMutableArray array];
        for (NSDictionary *dic in storiesArr) {
            StoryModel *model = [[StoryModel alloc] initWithDictionary:dic];
            [tempArr addObject:model];
        }
        [self setValue:tempArr forKey:@"stories"];
        [self setValue:jsonDic[@"background"] forKey:@"imageURLStr"];
        [self setValue:jsonDic[@"name"] forKey:@"name"];
        [self setValue:jsonDic[@"editors"] forKey:@"editors"];
    } failure:nil];
}
Copyright © 翻这个墙 2016 all right reserved,powered by Gitbook该文件修订时间: 2016-04-06 17:07:07