2011-08-26 181 views
2

是否可以約束標準的[[cell imageView]的寬度UITableViewCell? 我試過[[cell imageView] setFrame:CGRectMake(0, 0, 50, 50)]但它沒有效果。指定單元格的寬度imageView

[編輯]

否則,是否有可能在UIImageView添加它之前改變一個UIImage大小?

+0

有文學上成千上萬的類別實現縮放您的圖像。 – vikingosegundo

回答

2

如果您不能簡單地將圖像視圖的框架設置爲常規UITableViewCell,則可以創建一個UITableViewCell子類,並在-layoutSubviews中重新定位圖像視圖。

+0

謝謝!它運作良好 –

0

不,這是不可能的。

它的寬度不應該改變,因爲你必須創建UITableViewCell子類,你可以改變UITableViewCell中的imageview並根據它改變寬度。 或U可以使用

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

UIImageView *imgView; 
if(cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    imgView = [[UIImageView alloc] initWithFrame:(100,0,100,62)]; 
    [imgView setImage:[UIImage imageNamed:@"img.png"]]; 
    imgView.tag = 55; 
    [cell.contentView addSubview:imgView]; 
    [imgView release]; 
} 
else 
{ 
    imgView = (id)[cell.contentView viewWithTag:55]; 
} 
0

圖像鎖定大小:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
[imgview setImage:image]; 
[cell addSubview:imgView]; 
[imgView release]; 
+0

問題的目的是爲了避免這樣做。 –

0

這是內置的功能,爲每一個細胞提供ImageView的。除非它被分類,否則它的框架不能改變。

但是,您可以通過簡單的方法添加以下代碼添加任何自己imageViews的,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString* cellIdentifier = @"cellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(cell == nil) 
    { 
     cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
     UIImageView* *myView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
     [myView setImage:image]; 
     myView.tag = 123; 

     [cell.contentView addSubview:myView]; 
     [myView release]; 
    } 
    else 
    { 
     myView = (UIImageView*) [cell.contentView viewWithTag:123]; 
    } 
} 
6

您可以UIView的例子中使用.transform方法

cell.imageView.transform = CGAffineTransformMakeScale(0.65, 0.65); 

//如果你是UITableViewCell的子類,你也可以這樣做

- (void)layoutSubviews { 

    [super layoutSubviews]; 
    self.imageView.bounds = CGRectMake(10,5,65,65); 
    self.imageView.frame = CGRectMake(10,5,65,65); 
    self.imageView.contentMode = UIViewContentModeScaleAspectFit; 

    if (self.imageView.image) { 
     //TODO: adjust the textLabel and detailTextLabel 
    } 

} 
+1

經過四處尋找調整大小的ImageView或UIView與表格單元格使用cGAffineTransform方法。例如myUiImageView.transform = CGAffineTransformMakeScale(0.65,0.65);子類化和調用佈局子視圖對我無效。 –

+0

如果使用dequeueReusableCellWithIdentifier,則在單元格的圖像視圖上設置變換可能會重置:並且單元格將滾動出屏幕。 –

0

這裏是我的迅速解決方案th是問題

// 
// UITableVIewCellBasicWithFixedSizeImage.swift 
// 
// Created by Andrew Ashurow on 4/28/16. 
// Copyright © 2016. All rights reserved. 
// 

class UITableVIewCellBasicWithFixedSizeImage: UITableViewCell { 
     private let 
     IMAGE_VIEW_WIDTH:CGFloat  = 27, 
     TEXT_VIEW_LEFT_INSET:CGFloat = 15 

     override func layoutSubviews() { 
      super.layoutSubviews() 
      if let 
       imageView = imageView, 
       textLabel = textLabel{ 
       //set fixed image width 
       imageView.frame = CGRect(
        origin: imageView.frame.origin, 
        size: CGSize(
         width: IMAGE_VIEW_WIDTH, 
         height: imageView.frame.size.height 
        ) 
       ) 
       imageView.contentMode = .Center 
       imageView.autoresizingMask = .None 

       //calculate textLabel inset 
       textLabel.frame = CGRect(
        origin: CGPoint(
         x: imageView.frame.origin.x + imageView.frame.size.width + TEXT_VIEW_LEFT_INSET, 
         y: textLabel.frame.origin.y 
        ), 
        size: textLabel.frame.size 
       ) 

      } 
     } 
    }