appstore

前端之家收集整理的这篇文章主要介绍了appstore前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.新建一个single view工程

2.导入CFNetwork、MobileCoreServices、SystemConfiguration、libz.1.2.5.dylib系统库

3.导入SDWebImage、ASIHttpRequest开源库

4.修改ViewController.h

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    nc.title  = @"AppStore";
    self.window.rootViewController = nc;
    [nc release];

5.新建一个model类存储每个应用程序的信息

AppItem.h文件

@interface AppItem : NSObject

@property (nonatomic,copy) NSString *name,*detailInfo;
@property (nonatomic,retain) NSMutableArray *imageUrlArray;//存放大中小三种图片的网址

@end

AppItem.m文件

@implementation AppItem

@synthesize name = _name,detailInfo = _detailInfo,imageUrlArray = _imageUrlArray;

- (id)init
{
    if (self = [super init]) {
        _imageUrlArray = [[NSMutableArray alloc] init];
    }
    return  self;
}

- (void)dealloc
{
    _imageUrlArray = nil;
    [super dealloc];
}

@end

6.ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //初始化tableView和dataArray
    _dataArray = [[NSMutableArray alloc] init];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,320,416) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    //发送请求
    [self sendRequest];
}
//请求数据
- (void)sendRequest
{
    NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/cn/RSS/topgrossingipadapplications/limit=30/xml"];
    NSURL *url = [NSURL URLWithString:urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    [request startAsynchronous];
}

7.成功返回数据之后
- (void)requestFinished:(ASIHTTPRequest *)request
{
    if (_dataArray.count > 0) {
        [_dataArray removeLastObject];
    }
   
    NSString *content = [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding];
    //获取xml文档
    GdataxMLDocument *doc = [[GdataxMLDocument alloc] initWithXMLString:content options:0 error:nil];
    //初始化一个命名空间的字典
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"http://itunes.apple.com/RSS",@"im",@"http://www.w3.org/2005/Atom",@"xmlns",nil];
    //根据命名空间解析doc,获取一个数组,命名空间缺省的是xmlns
    NSArray *array = [doc nodesForXPath:@"//xmlns:entry" namespaces:dict error:nil];
    for (GdataxMLElement *ele in array) {
        
        //获取应用名
        NSArray *titleArray = [ele nodesForXPath:@"xmlns:title" namespaces:dict error:nil];
        GdataxMLElement *titleEle = titleArray.lastObject;
        AppItem *item = [[[AppItem alloc] init] autorelease];
        item.name = titleEle.stringValue;
        
        //获取应用图片
        NSArray *imageArray = [ele nodesForXPath:@"im:image" namespaces:dict error:nil];
        for (GdataxMLElement *imageEle in imageArray) {
            [item.imageUrlArray addObject:imageEle.stringValue];
        }
        [_dataArray addObject:item];
        
        //获取标题
        NSArray *subTitleArray = [ele nodesForXPath:@"xmlns:summary" namespaces:dict error:nil];
        GdataxMLElement *subTitleEle = subTitleArray.lastObject;
        item.detailInfo = subTitleEle.stringValue;
    }
    //刷新表格
    [_tableView reloadData];
}

8.关于tableview的设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ID"];
    }
    AppItem *item = [_dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = item.name;
    NSURL* imgUrl = [NSURL URLWithString:[item.imageUrlArray objectAtIndex:0]];
    NSData *imgData = [[NSData alloc] initWithContentsOfURL:imgUrl];
    UIImage *img = [UIImage imageWithData:imgData];
    cell.imageView.image = img;
    cell.detailTextLabel.text = item.detailInfo;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
原文链接:https://www.f2er.com/xml/299365.html

猜你在找的XML相关文章