------------ 输入一段文字返回该文字的高度 -----------
-(CGFloat)p_heightWithString:(NSString *)aString{
CGRect r =[aString boundingRectWithSize:CGSizeMake([TextTableViewCell backWidth], 2000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
return r.size.height;
}
--------------- 重定义 cell 高度 ---------------
OC实现方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identifier = @"cityCell";
CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//定义model对象 接收数据
CityModel * model = self.allModelArray[indexPath.row];
//图片标题
cell.city_title.text = model.title;
// 重计算图片标题高度
CGRect rect = [cell.city_title.text boundingRectWithSize:CGSizeMake(255, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldItalicMT" size:18.0]} context:nil];
cell.city_title.numberOfLines = 0;
cell.city_title.frame = CGRectMake(CGRectGetMinX(cell.city_title_page.frame), CGRectGetMaxY(cell.city_title_page.frame)+10, CGRectGetWidth(cell.city_title_page.frame)*0.70, rect.size.height);
cell.city_title.numberOfLines = 0;
[cell.contentView addSubview:cell.city_title];
return cell;
}
swift 实现方法:
//给cell设置自定义大小
func getHeightWithModel(model:MyModel) {
let str: String = model.content!
let rect = str.boundingRectWithSize(CGSizeMake(UIScreen.mainScreen().bounds.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(15)], context: nil)
// 给contentLabel重新根据字体多少赋值
self.contentLabel.frame = CGRectMake(10, 70, UIScreen.mainScreen().bounds.size.width - 20, rect.height)
// 设置点赞一排label的y轴
let height = CGRectGetMaxY(self.contentLabel.frame)
self.upLabel.frame = CGRectMake(10, height + 10, 40, 30)
self.upNumberLabel.frame = CGRectMake(60, height + 10, 40, 30)
self.discussLabel.frame = CGRectMake(110, height + 10, 40, 30)
self.discussNumberLabel.frame = CGRectMake(160, height + 10, 40, 30)
}
--------------- cell 重用方法 str--------------
// UITableView靠mutableSet来实现重⽤功能
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
//判断从重用池是否拿到cell,如果没有拿到,就创建。
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
}