新闻类简单写法

导入AFNetworking
记得开网

JieXi.h

#import 
typedef void(^RelustBlock)(NSDictionary *relust);
@interface JieXi : NSObject


+(void)HttpUrl:(NSString *)urlString preart:(NSDictionary *)preart relust:(RelustBlock)relust;

@end

JieXi.m

#import "JieXi.h"
#import "AFNetworking.h"
@implementation JieXi

+(void)HttpUrl:(NSString *)urlString preart:(NSDictionary *)preart relust:(RelustBlock)relust{
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    [manger GET:urlString parameters:preart progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        relust(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

@end

NextModel.h

#import 

@interface NextModel : NSObject

@property(nonatomic,copy)NSString *imageString,*titleString,*textString,*webString;

@end

NextModel.m

#import "NextModel.h"

@implementation NextModel

@end

NextTableViewCell.h

#import 
#import "NextModel.h"
@interface NextTableViewCell : UITableViewCell

@property(nonatomic,strong)UIImageView *imageVieww;

@property(nonatomic,strong)UILabel *titleLabel,*textLabele;

@property(nonatomic,strong)NextModel *nextModel;
@end

NextTableViewCell.m

#import "NextTableViewCell.h"

@implementation NextTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _imageVieww = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageVieww];
        
        _titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        
        _textLabele = [[UILabel alloc] init];
        [self.contentView addSubview:_textLabele];
        
        [self HHframe];
    }
    return self;
}

-(void)HHframe{
    
    _imageVieww.frame = CGRectMake(0, 0, 100, 100);
    
    _titleLabel.frame = CGRectMake(105, 5, 100, 20);
    
    _textLabele.frame = CGRectMake(105, 30, self.frame.size.width - 105, 60);
    _textLabele.numberOfLines = 0;
    
}

-(void)setNextModel:(NextModel *)nextModel{
    
    NSURL *url = [NSURL URLWithString:nextModel.imageString];
    
    UIImage *imagel = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    
    _imageVieww.image = imagel;
    
    _titleLabel.text = nextModel.titleString;
    
    _textLabele.text = nextModel.textString;
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

UIViewController.h 原
UIViewController.m

#import "ViewController.h"
#import "JieXi.h"
#import "NextModel.h"
#import "NextTableViewCell.h"
#import "OneViewController.h"
@interface ViewController ()
{
    UITableView *table;
}
@property(nonatomic,strong)NSMutableArray *mutableArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    [self HHtableView];
    [self HHjiexi];
}

-(void)HHjiexi{
    
    NSString *urlString = @"http://v.juhe.cn/weixin/query?key=57262e632820d778937ea674751f4872";
    
    _mutableArray = [[NSMutableArray alloc] init];
    
    [JieXi HttpUrl:urlString preart:nil relust:^(NSDictionary *relust) {
        NSDictionary *dict = relust[@"result"];
        NSArray *array = dict[@"list"];
        
        for (int i = 0; i< array.count; i++) {
            NSDictionary *dic = array[i];
            
            NextModel *model = [[NextModel alloc] init];
            
            model.imageString = dic[@"firstImg"];
            model.titleString = dic[@"source"];
            model.textString = dic[@"title"];
            model.webString = dic[@"url"];
            [_mutableArray addObject:model];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [table reloadData];
            });
        }
    }];
    
}


-(void)HHtableView{
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height -20)];
    
    table.delegate = self;
    table.dataSource = self;
    
    table.rowHeight = 100;
    
    [self.view addSubview:table];
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"cellOne";

    NextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (!cell) {
        cell = [[NextTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    NextModel *model = _mutableArray[indexPath.row];
    
    
    [cell setNextModel:model];
    
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OneViewController *one = [[OneViewController alloc] init];
    
    NextModel *model = _mutableArray[indexPath.row];

    one.webString = model.webString;
    
    [self presentViewController:one animated:YES completion:^{
        
    }];
    
}

@end

OneViewController.h

#import 

@interface OneViewController : UIViewController

@property(nonatomic,strong)NSString *webString;

@end

OneViewController.m

#import "OneViewController.h"

@interface OneViewController ()

@end

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_webString]];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

你可能感兴趣的:(新闻类简单写法)