2012-09-24 26 views
0

如何避免UITableViewCell中包含的UIImageView在每個表視圖單元格高度不同時展開?根據UILabel的文本長度,每行都有不同的高度。當UITableViewCell的高度增加時,圖像視圖的幀長是否爲常量?如何避免UITableViewCell中包含的UIImageView被展開?

- (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]; 
} 

回答

0

設置你的圖像以適當的值的contentMode屬性來禁止圖像視圖內的圖像的縮放和不高度關心圖像視圖本身的大小。

+0

我對內容模式使用了各種值。 UIViewContentModeScaleAspectFit,UIViewContentModeCenter,UIViewContentModeTop ....但沒有任何反應。仍然圖像視圖只需要增加高度。 –

+0

好吧,也許你發佈一些代碼呢?你如何創建圖像視圖,你分配的自動調整掩碼(或者你可能使用自動佈局)等等。 – mifki

0

創建,你可以保持不變UIImageView的框架自定義單元格。這將有助於它在整個的tableView細胞保持不變,即使你改變各種細胞

+0

要添加..我使用自定義單元格,只有在我的圖像視圖框架是恆定的。爲了確保圖像不會展開,我還使用索引路徑中行的單元格中相同的前一個常量幀重置圖像視圖的框架。 –

0
try this 

[cell.contentView addSubview:imageview]; 
相關問題