根据内容自动调节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
- 根据内容自动调节cell的高度
- 根据内容自动调整高度的cell
- textview根据字数自动调高度,cell高度自动调节
- div根据内容自动调节高度的解决方法
- div根据内容自动调节高度的解决方法
- 根据文本内容计算cell的高度
- ios根据内容设置cell的高度
- UITableView的Cell根据内容计算高度
- IOS 根据cell内容设置cell的高度
- 根据label的内容返回自定义cell的高度
- iframe 的高度根据内容自动调整
- iOS 根据文字内容设置cell 的高度
- UITextView根据内容、字体属性自动调节自己的大小
- cell根据文本标签内容自适应高度
- Iframe高度随内容自动调节
- Iframe高度随内容自动调节
- UITableViewCell根据内容自动高度
- UITableViewCell根据内容自动高度
- awk实战小例子
- 第十五周 项目1 哈希表及其运算实现
- dijit/layout/TabContainer的用法
- c++ primer plus 第六章变成练习 第2题
- CV大牛部分表格
- 根据内容自动调节cell的高度
- 第十三周项目3—Dijkstra算法的验证
- MTK_android项目驱动设置
- 反编译apk
- 慕课网:版本管理工具介绍—Git篇
- 第十一周项目 二叉树算法库
- AndroidStudio中使用DDMS工具
- storyboard中通过viewWithTag取tableview
- 华为手机不打印日志解决办法