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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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