2018-04-07

DataBase.h
导入JSONKit AFNetworking


#import@interface DataBase : NSObject

{

    NSDictionary *dict;

    NSMutableArray *arrays;

}

+ (instancetype)shareData;

- (void)getURL;

@end

DataBase.m

#import "DataBase.h"
#import "AFNetworking.h"
#import "JSONKit.h"
#define Monthly_Examination @"http://127.0.0.1/TextInforSale.json"

static DataBase *db = nil;

@implementation DataBase

+ (instancetype)shareData
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        db = [[self alloc]init];
    });
    return db;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (!db)
    {
        db = [super allocWithZone:zone];
    }
    return db;
}

- (id)mutableCopy
{
    return self;
}

- (id)copy
{
    return self;
}

- (void)getURL
{
    dict = [[NSDictionary alloc]init];
    arrays = [[NSMutableArray alloc]init];
    
//    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
//    manger.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
//    [manger GET:Monthly_Examination parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"成功");
//        dict = [responseObject objectFromJSONData];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"huxiaobo" object:dict];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"失败");
//    }];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    manger.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
    [manger GET:Monthly_Examination parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        dict = [responseObject objectFromJSONData];
        NSLog(@"%@",dict);
        arrays = [dict objectForKey:@"like"];
        NSLog(@"-----%@",arrays);
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"huxiaobo" object:arrays];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

@end

MyTableViewCell.h

#import <UIKit/UIKit.h>

@interface MyTableViewCell : UITableViewCell

@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UILabel *fromLab;
@property (nonatomic,strong)UILabel *likeNumLab;
@property (nonatomic,strong)UILabel *readNumlab;
@property (nonatomic,strong)UIImageView *images;

@end

MyTableViewCell.m

#import "MyTableViewCell.h"

@implementation MyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self getUI];
    }
    return self;
}

- (void)getUI
{
    _images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 414, 100)];
    
    _titleLab = [[UILabel alloc]initWithFrame:CGRectMake(50, 105, 300, 20)];
    _titleLab.backgroundColor = [UIColor orangeColor];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    
    _fromLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 130, 100, 20)];

    _likeNumLab = [[UILabel alloc]initWithFrame:CGRectMake(180, 130, 80, 20)];
    
    _readNumlab = [[UILabel alloc]initWithFrame:CGRectMake(250, 130, 50, 20)];
    
    
    
    
    [self.contentView addSubview:_titleLab];
    [self.contentView addSubview:_fromLab];
    
    [self.contentView addSubview:_likeNumLab];
    [self.contentView addSubview:_readNumlab];
    [self.contentView addSubview:_images];
}

@end

ViewController.m

#import "ViewController.h"
#import "DataBase.h"
#import "MyTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *arrays;
}
@property (nonatomic,strong)UITableView *tables;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(Data:) name:@"huxiaobo" object:nil];
    [[DataBase shareData]getURL];
    [self tables];
}

- (UITableView *)tables
{
    if (!_tables) {
        _tables = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tables.delegate = self;
        _tables.dataSource = self;
        [self.view addSubview:_tables];
    }
    return _tables;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrays.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellS = @"cell";
    
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellS];
    if (!cell)
    {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellS];
    }
    cell.titleLab.text = [arrays[indexPath.row] objectForKey:@"title"];
    cell.fromLab.text = [arrays[indexPath.row] objectForKey:@"from"];
    cell.likeNumLab.text = [arrays[indexPath.row] objectForKey:@"likeNum"];
    cell.readNumlab.text = [arrays[indexPath.row] objectForKey:@"readNum"];
    cell.images.image = [UIImage imageNamed:@"timg.jpeg"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)Data:(NSNotification *)notifi
{
    arrays = [NSMutableArray array];
    arrays = notifi.object;
    [_tables reloadData];
}

@end
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,242评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,953评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,299评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,709评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,723评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,236评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,629评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,594评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,135评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,156评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,285评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,914评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,600评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,073评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,203评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,798评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,339评论 2 365

推荐阅读更多精彩内容

  • 大致行文顺序: 1,努力应当,奋斗应该 2,无人不需努力 3,兰克•H•奈特( Frank H.Knight[1]...
    silvermorning阅读 181评论 0 0
  • 【为梦想而生.正能量】一款会飞的人工智能手机你们如何看http://mp.weixin.qq.com/s/pik_...
    兵哥黎辉阅读 285评论 0 0
  • i.m feel ok right 把money都装口袋 最近无聊到爆开始发嫌 思考怎么办我可以来钱 怎么每天过的...
    闯_9760阅读 64评论 0 0
  • 感恩早起给两个宝贝做饭,两个宝贝吃的很想。 感恩新年过后第一天开始正式工作! 感恩老客户信任!
    米朵天天阅读 156评论 0 0
  • 文:筠心 图:网络 我在毛滂篇,写朱祖谋有一点点偏爱乡人,今次要说的这位词人陈克,恰恰又是浙籍,一说临海,一说...
    筠心_阅读 832评论 0 6