ios tableviewcell cell根据内容自适应高度
今天继续在练习写应用 遇到了一个问题想做一个评论列表 可是评论的时候 每个人输入的文本高度都不同 所以不能写固定的行高
项目里的所有布局都是用代码写的因为没用过xib自学时没有学到这个 今天研究了一下感觉爽翻了
我是这样解决的
1.首先创建xib cell文件 删除自带的view 创建一个cell
2.用控件开始布局 由于是实验所以为的布局很糟糕见谅
在控制面板里设置好label的边距 将lines属性设置为0让其可以换行

3.在类里创建 uitableview 并将需要自定义内容的label标签连线到类里 创建属性
@property(strong,nonatomic)IBOutletUILabel*aaa;
- (void)viewDidLoad {
[superviewDidLoad];
UITableView*table = [[UITableViewalloc]initWithFrame:self.view.frame];
table.delegate=self;
table.dataSource=self;
[self.viewaddSubview:table];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return2;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString*Pid =@"Pid";
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:Pid];
if(!cell) {
NSArray*nib = [[NSBundlemainBundle]loadNibNamed:@"View"owner:selfoptions:nil];
cell = [nibobjectAtIndex:0];
}
NSArray*array =@[@"呵呵呵呵呵呵呵呵快乐撒到了撒娇的啦深刻的家大叔大叔大叔大叔的撒大声的",@"dakdsa;jdaskdjalskdjlaskdjlaskdjlaskjdklasjdkalsjdlkasdjlaksjdlaksjdlkasjdlkasjdlkasdjlaksdjlaskdjlaskjdlkasjdklasjdlkasjdklasjdklasjdaslkjdlaksdjasl"];
self.aaa.text= [arrayobjectAtIndex:indexPath.row];
returncell;
}
因为是测试用所以文本是乱写的
这个时候就会发现cell可以自动适应行高了

日记文章 记录一下 大神勿喷 代码方式目前没有测试