将这个文件夹导入工程,选择第一个文件配置 NSAppTransportSecurity下 NSAllowsArbitraryLoads
1.首先创建跟视图控制器
import "AppDelegate.h"
import "TableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[TableViewController new]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];return YES;
}
2.创建MODel模型的属性 Model.h
import <Foundation/Foundation.h>
@interface Model : NSObject
@property(nonatomic,copy)NSString * title;
@property(nonatomic,copy)NSString * summary;
@property(nonatomic,copy)NSString * picurl;
@end
- Model.m
import "Model.h"
@implementation Model
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
//重写方法
-(NSString* )description{
return [NSString stringWithFormat:@"title : %@,summary: %@",self.title,self.summary];
}
4.在TableViewCell.h中定义属性
import <UIKit/UIKit.h>
@interface TableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *title;
@property(nonatomic,strong)UILabel *summary;
@property(nonatomic,strong)UIImageView *picUrl;
@end
5.在TableViewCell.m
import "TableViewCell.h"
@implementation TableViewCell
-
(void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 40)]; _title.backgroundColor = [UIColor cyanColor]; _title.textColor = [UIColor blackColor]; _title.font = [UIFont systemFontOfSize:12]; _title.numberOfLines = 0; _title.lineBreakMode = UILineBreakModeCharacterWrap; [self.contentView addSubview:_title]; _summary = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 250, 80)]; _summary.backgroundColor = [UIColor cyanColor]; _summary.textColor = [UIColor blackColor]; _summary.font = [UIFont systemFontOfSize:12]; _summary.numberOfLines = 0; _summary.lineBreakMode = UILineBreakModeCharacterWrap; [self.contentView addSubview:_summary]; _picUrl = [[UIImageView alloc] initWithFrame:CGRectMake(270, 10, 150, 140)]; _picUrl.backgroundColor = [UIColor purpleColor]; [self.contentView addSubview:_picUrl];
}
return self;
}
6.在TableViewCell.m
import "TableViewController.h"
import "TableViewCell.h"
import "Model.h"
import "UIImageView+WebCache.h"
@interface TableViewController ()
@property(nonatomic,strong)NSMutableArray * dataArray;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
//创建数组
self.dataArray = [NSMutableArray array];
//网络解析
[self request];
}
pragma mark --解析
-(void)request{
NSURL * url = [NSURL URLWithString:@"[http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213](http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213)"];
//创建啊请求的实例
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
//接收数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//创建字典
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
for (NSDictionary * dic in dict[@"news"]) {
Model * model = [Model new];
[model setValuesForKeysWithDictionary:dic];
NSLog(@"%@",model.title);
[self.dataArray addObject:model];
}
//刷新数据
[self.tableView reloadData];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
pragma mark - Table view data source
-
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
} (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//取消cell点击效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Model *model = self.dataArray[indexPath.row];
cell.title.text = model.title;
cell.summary.text = model.summary;
//cell.picUrl.image = [UIImage imageNamed:@"1.png"];
[cell.picUrl sd_setImageWithURL:[NSURL URLWithString:model.picurl]];
return cell;
}