#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@end
#import "ViewController.h"
#import "CZVideo.h"
#import "CZParseVideoXml.h"
#import "CZVideoCell.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSArray *data;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CZVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZVideoCell" forIndexPath:indexPath];
cell.video = self.data[indexPath.row];
return cell;
}
- (void)loadData {
NSURL *url = [NSURL URLWithString:@"http://localhost/videos.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:15];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
self.data = [CZParseVideoXml parseWithData:data];
NSLog(@"%@",self.data);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"刷新UI%@",[NSThread currentThread]);
[self.tableView reloadData];
});
}];
}
@end
#import <Foundation/Foundation.h>
@interface CZVideo : NSObject
@property (nonatomic, copy) NSString *videoId;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *length;
@property (nonatomic, copy) NSString *videoURL;
@property (nonatomic, copy) NSString *imageURL;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *teacher;
@property (nonatomic, strong) NSURL *fullImageURL;
@property (nonatomic, copy) NSString *time;
@end
#import "CZVideo.h"
#define CZBaseURL [NSURL URLWithString:@"http://localhost/"]
@implementation CZVideo
- (void)setImageURL:(NSString *)imageURL {
_imageURL = imageURL;
_fullImageURL = [NSURL URLWithString:imageURL relativeToURL:CZBaseURL];
}
- (void)setLength:(NSString *)length {
_length = length;
NSInteger time = [length integerValue];
self.time = [NSString stringWithFormat:@"%02zd:%02zd:%02zd",time / 3600,time % 3600 / 60,time % 60];
}
@end
#import <Foundation/Foundation.h>
@interface CZParseVideoXml : NSObject
+ (NSArray *)parseWithData:(NSData *)data;
@end
#import "CZParseVideoXml.h"
#import "CZVideo.h"
@interface CZParseVideoXml ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *videos;
@property (nonatomic, strong) CZVideo *currentVideo;
@property (nonatomic, strong) NSMutableString *elementString;
@property (nonatomic, copy) void(^finished)(NSArray *);
@end
@implementation CZParseVideoXml
+ (NSArray *)parseWithData:(NSData *)data{
NSXMLParser *xml = [[NSXMLParser alloc]initWithData:data];
CZParseVideoXml *parse = [[CZParseVideoXml alloc]init];
xml.delegate = parse;
[xml parse];
return parse.videos.copy;
}
#pragma mark - xml 解析代理
- (void)parserDidStartDocument:(NSXMLParser *)parser {
self.videos = [NSMutableArray array];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(nonnull NSDictionary<NSString *,NSString *> *)attributeDict {
if ([elementName isEqualToString:@"video"]) {
self.currentVideo = [[CZVideo alloc]init];
self.currentVideo.videoId = attributeDict[@"videoId"];
}
[self.elementString setString:@""];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[self.elementString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"video"]) {
[self.videos addObject:self.currentVideo];
}else if (![elementName isEqualToString:@"videos"]) {
[self.currentVideo setValue:self.elementString forKey:elementName];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
if (self.finished) {
self.finished (self.videos.copy);
}
}
- (NSMutableString *)elementString {
if (_elementString == nil) {
_elementString = [NSMutableString string];
}
return _elementString;
}
@end
#import <UIKit/UIKit.h>
@class CZVideo;
@interface CZVideoCell : UITableViewCell
@property (nonatomic, strong) CZVideo *video;
@end
#import "CZVideoCell.h"
#import "CZVideo.h"
#import "UIImageView+WebCache.h"
@interface CZVideoCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *lengthLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *teacherLabel;
@end
@implementation CZVideoCell
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (void)setVideo:(CZVideo *)video {
_video = video;
[self.iconView sd_setImageWithURL:_video.fullImageURL placeholderImage:[UIImage imageNamed:@"user_default"] options:SDWebImageRetryFailed | SDWebImageLowPriority];
self.nameLabel.text = _video.name;
self.lengthLabel.text = _video.time;
self.descLabel.text = _video.desc;
self.teacherLabel.text = _video.teacher;
}
@end