2018-04-07

DataBase.h
導(dǎo)入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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末依疼,一起剝皮案震驚了整個(gè)濱河市季稳,隨后出現(xiàn)的幾起案子脱茉,更是在濱河造成了極大的恐慌朱躺,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逝嚎,死亡現(xiàn)場(chǎng)離奇詭異挤牛,居然都是意外死亡忽匈,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門方淤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钉赁,“玉大人,你說我怎么就攤上這事携茂∧悴龋” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵讳苦,是天一觀的道長(zhǎng)带膜。 經(jīng)常有香客問我,道長(zhǎng)鸳谜,這世上最難降的妖魔是什么膝藕? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮咐扭,結(jié)果婚禮上束莫,老公的妹妹穿的比我還像新娘。我一直安慰自己草描,他們只是感情好览绿,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著穗慕,像睡著了一般饿敲。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上逛绵,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天怀各,我揣著相機(jī)與錄音,去河邊找鬼术浪。 笑死瓢对,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的胰苏。 我是一名探鬼主播硕蛹,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了法焰?” 一聲冷哼從身側(cè)響起秧荆,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎埃仪,沒想到半個(gè)月后乙濒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卵蛉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年颁股,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片傻丝。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡豌蟋,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出桑滩,到底是詐尸還是另有隱情梧疲,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布运准,位于F島的核電站幌氮,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏胁澳。R本人自食惡果不足惜该互,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望韭畸。 院中可真熱鬧宇智,春花似錦、人聲如沸胰丁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽锦庸。三九已至机蔗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間甘萧,已是汗流浹背萝嘁。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留扬卷,地道東北人牙言。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像怪得,于是被迫代替她去往敵國(guó)和親咱枉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子卑硫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 大致行文順序: 1,努力應(yīng)當(dāng)庞钢,奮斗應(yīng)該 2,無人不需努力 3因谎,蘭克?H?奈特( Frank H.Knight[1]...
    silvermorning閱讀 179評(píng)論 0 0
  • 【為夢(mèng)想而生.正能量】一款會(huì)飛的人工智能手機(jī)你們?nèi)绾慰磆ttp://mp.weixin.qq.com/s/pik_...
    兵哥黎輝閱讀 282評(píng)論 0 0
  • i.m feel ok right 把money都裝口袋 最近無聊到爆開始發(fā)嫌 思考怎么辦我可以來錢 怎么每天過的...
    闖_9760閱讀 60評(píng)論 0 0
  • 感恩早起給兩個(gè)寶貝做飯基括,兩個(gè)寶貝吃的很想。 感恩新年過后第一天開始正式工作财岔! 感恩老客戶信任风皿!
    米朵天天閱讀 155評(píng)論 0 0
  • 文:筠心 圖:網(wǎng)絡(luò) 我在毛滂篇,寫朱祖謀有一點(diǎn)點(diǎn)偏愛鄉(xiāng)人匠璧,今次要說的這位詞人陳克桐款,恰恰又是浙籍,一說臨海夷恍,一說...
    筠心_閱讀 828評(píng)論 0 6