#import "ForthTableViewController.h"
#import "CartoonModel.h"
#import "CartoonCell.h"
#import "GDataXMLNode.h"
@interface ForthTableViewController ()
@property (nonatomic, retain)NSMutableArray * dataArray;
@end
@implementation ForthTableViewController
static NSString * cellReuse = @"cartoonCellReuse";
- (void)viewDidLoad {
[super viewDidLoad];
//解析文件
[self addParseWork];
}
- (void)addParseWork{
self.dataArray = [NSMutableArray array];
NSString * path = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
NSData * data = [NSData dataWithContentsOfFile:path];
//3.创建GDataXMLDocument对象,此时XML文件内所有的节点以树的形式存在GDataXMLDocument
NSError * error;
GDataXMLDocument * xmlDocument = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
//4.获取根节点,根节点里面包含了XML的所有信息
GDataXMLElement * rootElement = [xmlDocument rootElement];
//5.创建解析出来的漫画数组
self.dataArray = [NSMutableArray array];
//便利根节点的所有子节点
for (GDataXMLElement * subElement in rootElement.children) {
if ([subElement.name isEqual:@"results"]) {
//便利标签AlbumInfo里面的所有节点
for (GDataXMLElement * ele in subElement.children) {
CartoonModel * cart = [CartoonModel new];
for (GDataXMLElement * contactElement in ele.children) {
//使用kvc对per的属性赋值
[cart setValue:contactElement.stringValue forKeyPath:contactElement.name];
}
//将解析赋值完的漫画添加到数组中
[_dataArray addObject:cart];
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CartoonCell * cell = [tableView dequeueReusableCellWithIdentifier:cellReuse forIndexPath:indexPath];
CartoonModel * model = _dataArray[indexPath.row];
cell.cartoonModel = model;
NSString * str = model.desc;
cell.descLabel.frame = CGRectMake(10, 40, self.view.frame.size.width-20, [self textHeight:str]);
return cell;
}
//设置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CartoonModel * model = _dataArray[indexPath.row];
NSString * str = model.desc;
return [self textHeight:str]+50;
}
//计算文本高度
-(CGFloat)textHeight: (NSString *)string{
CGRect newRect = [string boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
return newRect.size.height;
}
@end
iOS的 tableView的自适应高度, XML 的 DOM 解析
最新推荐文章于 2016-04-30 11:07:09 发布