2014-07-21 49 views
1

内细胞高度我用UITableView显示来自数据库的项目,我已经创建了自定义UITableViewCell其中有几个标签。我最大的标签是名称说明。
每个单元可以有不同的高度,但我想不出如何设置动态的单元格高度。
我得到的是三个点在标签和高度单元的结束总是约50
如何使基于它里面的标签上单元格高度?
如何设置基础上的UILabel自定义单元格

#define FONT_SIZE 14.0f 
#define CELL_CONTENT_WIDTH 320.0f 
#define CELL_CONTENT_MARGIN 10.0f 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *text = _orderProperty.Description; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] 
        constrainedToSize:constraint 
         lineBreakMode:NSLineBreakByWordWrapping]; 

    CGFloat height = MAX(size.height, 44.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 

UPDATE

static NSString *CellIdentifier = @"orderDetailCell"; 

    OrderDetailCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil){ 
     [tableView registerNib:[UINib nibWithNibName:@"OrderDetailCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    } 

    OrderProperty* item = [_list objectAtIndex:indexPath.row]; 

    cell.Title.text = item.Title; 

    NSString* _description = item.Description; 

    cell.Description.text = _description; 
+0

看到此链接的用户 “2014” 回答http://stackoverflow.com/questions/17358322/tableview-convertpoint-fromview-奇怪的行为和解决方法/ 17359673#17359673 –

回答

0

你可以尝试这样的事情

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int rowHeight =0.0f; 
    NSString *stringToSize=[NSString stringWithFormat:@"%@",longStringValue]; 
    CGSize size = [stringToSize 
        sizeWithFont:[UIFont systemFontOfSize:15.0f] 
        constrainedToSize:CGSizeMake(300, 6000) 
        lineBreakMode:NSLineBreakByWordWrapping]; 
    rowHeight = size.height+10; 
    return rowHeight; 
} 

你可以从阵列得到longStringValue您正在使用的数据源为您的桌面

0

sizeWithFont现不推荐使用,请使用以下方法根据标签内的文字设置销售高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    OrderProperty* item = [_list objectAtIndex:indexPath.row]; 

    NSString *text = item.Description; 

    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Helvetica" size:13] forKey:NSFontAttributeName]; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize expectedLabelSize = [content boundingRectWithSize: constraint options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size; 

    return expectedLabelSize.height+CELL_CONTENT_MARGIN * 2); 
} 

显然你需要调整字符串的属性,以适应你正在使用的任何东西。这些只是用于计算电池所需的高度。

+0

它接近,这是接近解决方案。当我用表格加载视图时,一些长的字符串单元格被截断。但是在向下滚动后,它会修正高度。为什么会发生? – 1110

+0

你可以忍受你的cellForRowAtIndexPath方法吗?通常要检索该单元格中的文本,您将获得基于indexPath的文本。你的cellForRow细节可能会帮助我看看是否是这种情况 –

+0

好的,谢谢你。如果你可以添加一行OrderProperty *项目= [_list objectAtIndex:索引路径 - 这将意味着电池拿起正确的单元格中正确的文本。让我知道它现在是否有效。干杯 –

0

您可以使用下面的NSString范畴,它使用NSTextContainerNSTextContainer计算确切大小的文字。

NSString+Calculation.m

而且你的最终代码会是这样:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *text = _orderProperty.Description; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [text usedSizeForMaxWidth:constraint.width 
            withFont:[UIFont systemFontOfSize:FONT_SIZE]]; 

    CGFloat height = MAX(size.height, 44.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 
相关问题