標籤:style color os io for cti 代碼 div
在做tableView的時候,我們有時候需要根據cell的高度動態來調整,最近在網上看到一段代碼不錯,跟大家Share一下。
在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
類中擷取cell的高度:
CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX); cell.textLabel.text = @"12345678900123456789"; cell.userInteractionEnabled = NO; cell.textLabel.numberOfLines = 0; CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap]; CGRect rect = cell.frame; rect.size.height = requiredSize.height+5; cell.frame = rect;
這時候擷取到了cell的高度,然後在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
類中改變cell的高度:
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; NSLog(@"cell height %f",cell.frame.size.height); return cell.frame.size.height;
這樣以來cell的高度就根據cell裡label的內容自動改變啦。
其主要出發點就是我有一個label,然後我要把這個label展示出來,我根據字型的大小還有行數來擷取一個高度,這樣cell的高度就有啦。