如何避免UITableViewCell中包含的UIImageView被扩展?

时间:2012-09-24 10:23:56

标签: iphone

当每个表格视图单元格的高度不同时,如何避免UITableViewCell中包含的UIImageView被展开?我根据UILabel的文本长度,每行的高度可变。当UITableViewCell的高度增加时,图像视图s image height also increases. How to keep image view的帧常量?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
       NSString *text = [self.myArray objectAtIndex:indexPath.row];

        if(text.length!=0)
        {    

        CGSize constraint = CGSizeMake(212, LONG_MAX);

        CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Arial" size:14.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

        CGFloat height = size.height;

      return (71.0 + height); //this gives the row height
        }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MyXYZ *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyXYZ" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[MyXYZ class]]) {
                cell = (MyXYZ *)currentObject;
]                break;
            }
        }
    }  

 ((HJManagedImageV *)cell.myImageView).url = [NSURL URLWithString:myImageUrl]; 
}   

3 个答案:

答案 0 :(得分:0)

创建一个自定义单元格,您可以在其中保持UIImageView的框架不变。这将有助于它在整个tableView单元格中保持相同,即使您改变各种单元格的高度

答案 1 :(得分:0)

try this

[cell.contentView addSubview:imageview];

答案 2 :(得分:0)

将图片视图的contentMode属性设置为适当的值,以禁止在图像视图中缩放图像,而不关心图像视图本身的大小调整。