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
@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 ()
{
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