#define PATH @"http://www.oschina.net/action/api/tweet_list?uid=0&pageIndex=0&pageSize=10"
#import "ViewController.h"
#import "TweetModel.h" //数据模型
#import "AFNetworking.h" //第三方库
#import "GDataXMLNode.h" //第三方库
#import "UIImageView+WebCache.h" //第三方库
#import "TweetCell.h" //用xib自定义的cell类
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray * _dataArr;
UITableView * _tableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataArr=[[NSMutableArray alloc]init];
[self createUI];
[self startNet];
}
-(void)createUI
{
_tableView=[[UITableView alloc]initWithFrame:self.view.frame];
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.separatorColor=[UIColor redColor];
[self.view addSubview:_tableView];
[_tableView registerNib:[UINib nibWithNibName:@"TweetCell" bundle:nil] forCellReuseIdentifier:@"guo"];
}
-(void)startNet
{
AFHTTPRequestOperationManager * manage=[AFHTTPRequestOperationManager manager];
manage.responseSerializer=[AFHTTPResponseSerializer serializer];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
[manage GET:PATH parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
[self xml:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"网络连接出错");
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}];
}
-(void)xml:(NSData *)data
{
GDataXMLDocument * document=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
//查询document里所有结点为tweet的结点,//相对路径
NSArray * tweetArr=[document nodesForXPath:@"//tweet" error:nil];
//结点转模型
for (GDataXMLElement * tweetEle in tweetArr) {
//创建结点
TweetModel * tm=[[TweetModel alloc]init];
//循环遍历tweet结点里所有的子结点
for (GDataXMLElement * ele in [tweetEle children]) {
//用kvc赋值
[tm setValue:ele.stringValue forKey:ele.name];
}
[_dataArr addObject:tm];
}
[_tableView reloadData];
}
#pragma mark -tableview的实现
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
TweetModel * tm=_dataArr[indexPath.row];
CGSize size=[tm.body boundingRectWithSize:CGSizeMake(210-2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:NULL].size;
if (tm.imgSmall.length) {
return (280-90+size.height+10);
}else
return 280-90+size.height+10-100-10;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//有复用的功能,如果提前注册这样写就可以
TweetCell * cell=[tableView dequeueReusableCellWithIdentifier:@"guo" forIndexPath:indexPath];
TweetModel * tm=[_dataArr objectAtIndex:indexPath.row];
[cell.portraintView sd_setImageWithURL:[NSURL URLWithString:tm.portrait]];
cell.authorLabel.text=tm.author;
cell.bodyLabel.text=tm.body;
//计算一个字符串完整展示出来所需要占用的frame,
//第一个参数:计算结果的限制(一般只限制宽度)
//第二个参数:固定写法
//第三个参数:计算frame时文字使用的属性
//返回值是frame但是由于不需要x,y的值所以用size来接,确定宽高就可以了
CGSize size= [tm.body boundingRectWithSize:CGSizeMake(210-2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cell.bodyLabel.font} context:NULL].size;
//修改label的高度
CGRect frame=cell.bodyLabel.frame;
frame.size.height=size.height+2; //+2是为了防止计算机自己计算精度有问题,防止label中的文字出现......
cell.bodyLabel.frame=frame;
//判断有没有图片
if (tm.imgSmall.length) {
cell.imgView.hidden=NO;
[cell.imgView sd_setImageWithURL:[NSURL URLWithString:tm.imgSmall]];
//修改留言图片的frame
CGRect imageFrame=cell.imgView.frame;
imageFrame.origin.y=frame.origin.y+frame.size.height+10; //?????
cell.imgView.frame=imageFrame;
frame=imageFrame;
}else
{
//如果没有图片就不显示图片
cell.imgView.hidden=YES;
}
//留言日期位置的判断,只需要更改y就可以
CGRect pubFrame=cell.pubDateLabel.frame;
pubFrame.origin.y=frame.origin.y+frame.size.height+10; //?????
cell.pubDateLabel.frame=pubFrame;
cell.pubDateLabel.text=tm.pubDate;
return cell;
}
@end