先看效果(數(shù)據(jù)都是為了測(cè)試杂靶,別在意哈?)
屏幕快照 2016-11-24 09.03.51.png
因?yàn)槲疫@是在一個(gè)cell里面做的,并且得讓他隨著數(shù)據(jù)的多少自動(dòng)適應(yīng)高度(也就是說(shuō)數(shù)據(jù)顯示出來(lái)多高酱鸭,cell就得創(chuàng)建多高吗垮,但這是個(gè)悖論, 因?yàn)閏ell創(chuàng)建好了就沒(méi)法改變高度凹髓,但這些標(biāo)簽是cell創(chuàng)建好了才樂(lè)意傳數(shù)據(jù)進(jìn)去創(chuàng)建烁登,燒了我好多腦細(xì)胞),好了不多說(shuō)了蔚舀,showtime饵沧。
cell的.h
@interface WartFallTableViewCell : UITableViewCell
-(void)setCellWithArr:(NSArray *)arr;
@end
這沒(méi)什么可說(shuō)的
cell的.m
@implementation WartFulTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
//關(guān)鍵方法(其實(shí)思路理清楚就不難)
-(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;//一個(gè)字符所占寬度
CGFloat sum_w_befor = sum_w;
for (int i = 0; i<arr.count; i++) {
//這是隨機(jī)邊框的顏色
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;
}
//提取數(shù)據(jù)的大小
NSString *str = arr[i];
NSInteger numberOfStr = str.length;
//把標(biāo)簽的寬度加入總寬中
sum_w_befor = sum_w;//沒(méi)有累加前的總寬,為了給label賦位置赌躺。
sum_w += W_distance + numberOfStr*one_width;//累加后的寬為了判斷是否折行捷泞。
if (sum_w>kScreenWidth) {//如果寬度超過(guò)了屏幕寬 則折行
sum_w = W_distance;
sum_w_befor = sum_w;
sum_w += W_distance + numberOfStr*one_width;//折行后數(shù)據(jù)的處理(難點(diǎn),少了我不少腦細(xì)胞)
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自動(dòng)適應(yīng)試的各種方法寿谴,但并沒(méi)什么卵用锁右。
//self.backLabelHeight.constant = sum_h; //這是我用xib做的一個(gè)label的約束拉出來(lái)的但cell創(chuàng)建以后改變約束并沒(méi)有什么效果了。
// self.cellHeight = sum_h;//這是我給他添加的類(lèi)目讶泰。(這個(gè)方法為了配合這個(gè)表里邊的其他cell咏瑟,因?yàn)槠渌鹀ell都是拉約束高度自適應(yīng)的,寶寶心里苦啊痪署,就這個(gè)cell不行)
}
看了上面代碼码泞,其實(shí)關(guān)鍵的已經(jīng)完了,但有一個(gè)坑就是cell高度不能隨著數(shù)據(jù)的多少自適應(yīng)狼犯,所以只能在UITableView的代理里邊設(shè)定了(蛋疼S嗔取!C跎)
這是創(chuàng)建的部分
-(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;
}
這是返回高度的 在這里再計(jì)算一遍(其實(shí)也可以用代理傳回來(lái)宋舷,但我懶的用,直接代碼復(fù)制過(guò)來(lái)瓢姻。祝蝠。。囧!)
- (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;//一個(gè)字符所占寬度
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) {//如果寬度超過(guò)了屏幕寬 則折行
sum_w = W_distance;
sum_w += W_distance + numberOfStr*one_width;
sum_h+= H_distance+autoScaleH(30);
}
}
sum_h+=H_distance;
return sum_h;
}
到此結(jié)束了细溅,心碎之旅!(對(duì)了儡嘶,哪位大神喇聊,知道cell創(chuàng)建以后,再修改cell高度的方法給我說(shuō)說(shuō))
求虐蹦狂,求指點(diǎn)3衅!!跪求鸥咖!