先看效果(數據都是為了測試,別在意哈?)
屏幕快照 2016-11-24 09.03.51.png
因為我這是在一個cell里面做的,并且得讓他隨著數據的多少自動適應高度(也就是說數據顯示出來多高,cell就得創建多高,但這是個悖論, 因為cell創建好了就沒法改變高度,但這些標簽是cell創建好了才樂意傳數據進去創建,燒了我好多腦細胞),好了不多說了,showtime。
cell的.h
@interface WartFallTableViewCell : UITableViewCell
-(void)setCellWithArr:(NSArray *)arr;
@end
這沒什么可說的
cell的.m
@implementation WartFulTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
//關鍵方法(其實思路理清楚就不難)
-(void)setCellWithArr:(NSArray *)arr{
CGFloat H_distance = autoScaleH(15.0f);//間距——高
CGFloat W_distance = autoScaleW(13.0f);//間距——寬
CGFloat sum_w = W_distance;//總寬
CGFloat sum_h = 0;//總高
CGFloat one_width = kScreenWidth/2/10.0f;//一個字符所占寬度
CGFloat sum_w_befor = sum_w;
for (int i = 0; i<arr.count; i++) {
//這是隨機邊框的顏色
NSInteger j = arc4random()%4;
UIColor *bordercolor = kBlueColor;
switch (j) {
case 1:
bordercolor = kBlueColor;
break;
case 2:
bordercolor = kYellowColor;
break;
case 3:
bordercolor = kRedColor;
break;
default:
break;
}
//提取數據的大小
NSString *str = arr[i];
NSInteger numberOfStr = str.length;
//把標簽的寬度加入總寬中
sum_w_befor = sum_w;//沒有累加前的總寬,為了給label賦位置。
sum_w += W_distance + numberOfStr*one_width;//累加后的寬為了判斷是否折行。
if (sum_w>kScreenWidth) {//如果寬度超過了屏幕寬 則折行
sum_w = W_distance;
sum_w_befor = sum_w;
sum_w += W_distance + numberOfStr*one_width;//折行后數據的處理(難點,少了我不少腦細胞)
sum_h+= H_distance+autoScaleH(30);
}
UILabel *textLabl = [[UILabel alloc]initWithFrame:CGRectMake(sum_w_befor, sum_h+H_distance,numberOfStr*one_width, autoScaleH(30))];
textLabl.textAlignment = NSTextAlignmentCenter;
textLabl.layer.cornerRadius = autoScaleH(12);
textLabl.layer.masksToBounds = YES;
textLabl.layer.borderWidth = 1;
textLabl.layer.borderColor =bordercolor.CGColor;
textLabl.font = [UIFont systemFontOfSize:autoScaleW(15)];
textLabl.text = str;
[self addSubview:textLabl];
}
sum_h+=H_distance+H_distance+autoScaleH(30);
// 以下兩行是我為了讓cell自動適應試的各種方法,但并沒什么卵用。
//self.backLabelHeight.constant = sum_h; //這是我用xib做的一個label的約束拉出來的但cell創建以后改變約束并沒有什么效果了。
// self.cellHeight = sum_h;//這是我給他添加的類目。(這個方法為了配合這個表里邊的其他cell,因為其他cell都是拉約束高度自適應的,寶寶心里苦啊,就這個cell不行)
}
看了上面代碼,其實關鍵的已經完了,但有一個坑就是cell高度不能隨著數據的多少自適應,所以只能在UITableView的代理里邊設定了(蛋疼!!!)
這是創建的部分
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
WartFulTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WartFulTableViewCell" forIndexPath:indexPath];
// 這些都是痛啊
// cell = [[WartFulTableViewCell alloc]initWithArr:self.dataModel.yach_project];
[cell setCellWithArr:self.dataModel.yach_project];
return cell;
}
這是返回高度的 在這里再計算一遍(其實也可以用代理傳回來,但我懶的用,直接代碼復制過來。。。囧!)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat H_distance = autoScaleH(15.0f);//間距——高
CGFloat W_distance = autoScaleW(13.0f);//間距——寬
CGFloat sum_w = 0;//總寬
CGFloat sum_h = H_distance+autoScaleH(30);//總高
CGFloat one_width = kScreenWidth/2/10.0f;//一個字符所占寬度
for (int i = 0; i<self.dataModel.yach_project.count; i++) {
NSString *str = self.dataModel.yach_project[i];
NSInteger numberOfStr = str.length;
sum_w+=W_distance + numberOfStr*one_width;//累加后的寬為了判斷是否折行。
if (sum_w>kScreenWidth) {//如果寬度超過了屏幕寬 則折行
sum_w = W_distance;
sum_w += W_distance + numberOfStr*one_width;
sum_h+= H_distance+autoScaleH(30);
}
}
sum_h+=H_distance;
return sum_h;
}
到此結束了,心碎之旅!(對了,哪位大神,知道cell創建以后,再修改cell高度的方法給我說說)
求虐,求指點!!跪求!