根据内容自动调节cell的高度

来源:互联网 发布:mysql 获取当前毫秒数 编辑:程序博客网 时间:2024/05/02 01:13

#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 {

    [superviewDidLoad];


    _dataArr=[[NSMutableArrayalloc]init];

    

    [self createUI];

    [self startNet];

}

-(void)createUI

{

    _tableView=[[UITableViewalloc]initWithFrame:self.view.frame];

    _tableView.delegate=self;

    _tableView.dataSource=self;

    _tableView.separatorColor=[UIColorredColor];

    

    [self.view addSubview:_tableView];

    

    

    [_tableViewregisterNib:[UINibnibWithNibName:@"TweetCell"bundle:nil]forCellReuseIdentifier:@"guo"];

}

-(void)startNet

{

    AFHTTPRequestOperationManager * manage=[AFHTTPRequestOperationManagermanager];

    manage.responseSerializer=[AFHTTPResponseSerializerserializer];

    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

    

    [manage GET:PATHparameters: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=[[GDataXMLDocumentalloc]initWithData:dataoptions:0error:nil];

    

    //查询document里所有结点为tweet的结点,//相对路径

    NSArray * tweetArr=[document nodesForXPath:@"//tweet" error:nil];

    

    //结点转模型

    for (GDataXMLElement * tweetElein tweetArr) {

        //创建结点

        TweetModel * tm=[[TweetModelalloc]init];

        //循环遍历tweet结点里所有的子结点

        for (GDataXMLElement * elein [tweetEle children]) {

            //kvc赋值

            [tm setValue:ele.stringValueforKey: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.bodyboundingRectWithSize:CGSizeMake(210-2,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize: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=[tableViewdequeueReusableCellWithIdentifier:@"guo"forIndexPath:indexPath];

    

    TweetModel * tm=[_dataArrobjectAtIndex:indexPath.row];

    [cell.portraintView sd_setImageWithURL:[NSURLURLWithString:tm.portrait]];

    cell.authorLabel.text=tm.author;

    cell.bodyLabel.text=tm.body;

    

    //计算一个字符串完整展示出来所需要占用的frame,

    //第一个参数:计算结果的限制(一般只限制宽度)

    //第二个参数:固定写法

    //第三个参数:计算frame时文字使用的属性

    //返回值是frame但是由于不需要x,y的值所以用size来接,确定宽高就可以了

    CGSize size= [tm.bodyboundingRectWithSize:CGSizeMake(210-2,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOriginattributes:@{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:[NSURLURLWithString: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

0 0
原创粉丝点击