看看能不能帮忙。您可以创建一个新类扩展至 UITableViewCell,这意味着将 UITableViewCell 重写为您自己的单元格,命名为 MyTestCell。在这个单元格中,您调用创建您的属性,如标签和视图,并将它们添加到您的新单元格中。喜欢将其添加到 MyTestCell.h
@property (nonatomic, retain) UILable *myLable1;
@property (nonatomic, retain) UIView *mySubview1;
MyTestCell.m
_myLable1 = .....
_mySubview = .....
[self addSubview: _myLbale1];
[self addSubview: _mySubview1];
And when use, u can work like this
static NSString *CellIdentifier = @"MyCell";
MyTableViewCell *cell = [tableview dequeReuseID:CellIdentifier];
if (cell == nil) {
cell = [MyTableViewCell alloc] init.........
}
//And you can sign your property here in your cell
cell.myLable1 = ....
cell.myView1 = .....
return cell;
}
如果您添加到标签的字符串不同,请使标签.高度不同。你可以使用这样的代码
CGSize labelSize = [str sizeWithFont:[UIFont boldSystemFontOfSize:17.0f]
constrainedToSize:CGSizeMake(280, 100)
lineBreakMode:UILineBreakModeCharacterWrap]; //check your lableSize
UILabel *patternLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 157, labelSize.width, labelSize.height)];
patternLabel.text = str;
patternLabel.backgroundColor = [UIColor clearColor];
patternLabel.font = [UIFont boldSystemFontOfSize:17.0f];
patternLabel.numberOfLines = 0;// must have
patternLabel.lineBreakMode = UILineBreakModeCharacterWrap;// must have
将此添加到您的单元格中,并使其动态调整您的标签以及您的单元格的大小!而且您还必须为您的 tableView 行高动态设置高。(知道什么是动态调整大小吗?)请参阅:rewrite the method setMyLable1 in MyTableViewCell.m
-(void)setMyLable1:(UILable*)aLable
{
//in here when never your sign your alabel to your cell (like this : cell.myLable1) this method will be call and u can get the size of your string and set this label's height
//get string size StringSzie
[_myLable1 setFrame:CGRectMake(10,10,stringSize.width,stringSize.height)];
//And resize your cell as well
[self setFrame:CGRectMake(0,0,_myLable1.frame.size.width+20,_myLable1.frame.size.height+20)];
//done!!!
}
好的,您会为自己获得一个自动重新调整的单元格,并且您也必须在 tableView 中为您的行动态重置高度!!!!!!