iOS入门-43SDWebImage图片加载库

概述

重点
  • AFNetworking加载网络数据
  • SDWebImage加载网络图片
  • UITableview列表使用
实际操作过程中注意
  • 设置导航栏、根视图控制器
  • http协议如何配置使其能被使用
  • CocoaPosd配置第三方库

示例

配置http可以使用

iOS 9.0由于强制使用https , 所以之前使用的 http的连接 的应用如果不做特殊配制就都不可以运行了,为了解决这个问题要在工程的info.plit中添加如下配制。
在这里插入图片描述

引入第三方库参考“CocoaPods安装使用”

先看一下最终效果

在这里插入图片描述

示例代码:

AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain,nonatomic) UIWindow* window;

@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.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];
    ViewController* vc = [ViewController new];
    UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = navC;
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end
ViewController.h
#import <UIKit/UIKit.h>
//UITableViewDelegate,UITableViewDataSource:列表控件涉及的两个代理类,实现他们
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    //列表控件
    UITableView* _tableView;
    //数据源
    NSMutableArray* _arrayData;
    //加载按钮
    UIBarButtonItem* _btnLoadData;
    //编辑按钮
    UIBarButtonItem* _btnEdit;
}

@end
ViewController.m
#import "ViewController.h"
#import "AFNetworking.h"
#import "UIImageview+WebCache.h"
#import "Program.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"SDwebImage demo";
    //创建列表视图控件
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    //设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //设置自动调整尺寸
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    
    //模拟数据
    _arrayData = [NSMutableArray new];
    
    [self.view addSubview:_tableView];
    
    //数据加载按钮
    _btnLoadData = [[UIBarButtonItem alloc] initWithTitle:@"加载" style:UIBarButtonItemStylePlain target:self action:@selector(pressLoad)];
    self.navigationItem.rightBarButtonItem = _btnLoadData;
}

//点击按钮数据加载
-(void) pressLoad{
    [self loadDataFromNet];
}

//加载网络数据
-(void)loadDataFromNet{
    NSString* url = @"你的url地址";
    
    //使用AFNetworking进行网络请求
    AFHTTPSessionManager* session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript", nil];
    [session GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功了");
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSLog(@"rootDic==%@",responseObject);
            [self parseData:responseObject];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败了");
    }];
}

//解析网络数据:下面是手动一个一个来解析的,解析完成加入容器数组中
-(void)parseData:(NSDictionary*)dic{
    NSArray* arrayCategories = [dic objectForKey:@"categories"];
    
    NSDictionary* dicDategory = arrayCategories[1];
        
    NSArray* arrayDetail = [dicDategory objectForKey:@"detail"];
    
    for (int i=0; i<arrayDetail.count; i++) {
        NSDictionary* detail = [arrayDetail objectAtIndex:i];
        Program* program = [Program new];
        program.mName = [detail objectForKey:@"name"];
        program.mImageUrl = [detail objectForKey:@"other_info7"];
        [_arrayData addObject:program];
    }
    
    //刷新列表
    [_tableView reloadData];
}

//每一组中几条
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrayData.count;
}

//有几组
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//列表每个条目数据加载
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* strId = @"ID";
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:strId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strId];
    }
    
    //数组中取出解析好的数据,更新展示数据
    Program* program = _arrayData[indexPath.row];
    cell.textLabel.text = program.mName;
    NSURL* imageUrl = [NSURL URLWithString:program.mImageUrl];
    //注意如果不引入SDWebimage库,是不会有如下的sd_setImageWithURL方法的。
    [cell.imageView sd_setImageWithURL:imageUrl];
    
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

@end
Program.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Program : NSObject

@property(retain,nonatomic) NSString* mName;
@property(retain,nonatomic) NSString* mImageUrl;

@end

NS_ASSUME_NONNULL_END
json数据结构如下
{
    "categories": [
        {
            "id": "",
            "detail": [
                {
                    "id": "1",
                    "name": "哈哈哈",
                    "other_info7": "图片url"
                } 
             ]      
         },
         {
            "id": "",
            "detail": [
                {
                    "id": "222",
                    "name": "呵呵呵",
                    "other_info7": "图片url"
                } ,
                。。。。。省略。。。。。
             ]       
         },          
      ]              
 }                   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值