即使图像较小,如何使UITableViewCell的ImageView成为固定大小

Rob*_*ert 102 iphone objective-c uitableview uikit uiimageview

我有一堆用于细胞图像视图的图像,它们都不超过50x50.例如40x50,50x32,20x37 .....

当我加载表格视图时,文本不会排列,因为图像的宽度会有所不同.此外,我希望小图像出现在中心而不是左侧.

这是我在'cellForRowAtIndexPath'方法中尝试的代码

cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
cell.imageView.autoresizesSubviews = NO;
cell.imageView.contentMode = UIViewContentModeCenter;
cell.imageView.bounds = CGRectMake(0, 0, 50, 50);
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.image = [UIImage imageWithData: imageData];
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我尝试了一些东西,但它们都没有用.

Nil*_*nch 151

没有必要重写所有内容.我推荐这样做:

将其发布在自定义单元格的.m文件中.

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0,0,32,32);
}
Run Code Online (Sandbox Code Playgroud)

这应该很好地完成.:]

  • 如果我们不添加`UITableViewCell`的子类怎么办? (44认同)
  • 如果设置`self.imageView.bounds`,图像将居中. (27认同)
  • 由于标签未对齐,它对我不起作用. (11认同)
  • 这对我不起作用.图像仍然吞没整个imageView. (5认同)
  • @动静能量:子类化UITableViewCell是使其工作的主要技巧. (3认同)

Ger*_*sio 133

对于那些没有子类的人UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 [...]

      CGSize itemSize = CGSizeMake(40, 40);
      UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
      CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
      [cell.imageView.image drawInRect:imageRect];
      cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

 [...]
     return cell;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将大小设置为40x40.

斯威夫特2

    let itemSize = CGSizeMake(25, 25);
    UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale);
    let imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    cell.imageView?.image!.drawInRect(imageRect)
    cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

或者您可以使用@Tommy建议的另一种(未经测试)方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 [...]

      CGSize itemSize = CGSizeMake(40, 40);
      UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0)          
 [...]
     return cell;
}
Run Code Online (Sandbox Code Playgroud)

Swift 3+

let itemSize = CGSize.init(width: 25, height: 25)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale);
let imageRect = CGRect.init(origin: CGPoint.zero, size: itemSize)
cell?.imageView?.image!.draw(in: imageRect)
cell?.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!;
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

上面的代码是上面的Swift 3+版本.

  • 对于那些像我一样困惑的人,你需要在上下文开始之前设置图像.ie cell.imageView.image = [UIImage imageNamed:@"my_image.png"]; (6认同)
  • 这样昂贵的操作不应该是cellForRowAtIndexPath的一部分 (5认同)
  • 图像失真可以通过UIGraphicsBeginImageContextWithOptions(itemSize,NO,UIScreen.mainScreen.scale)修复; 而不是UIGraphicsBeginImageContext(itemSize); (3认同)
  • @GermanAttanasioRuiz在选择它再次调整大小的单元格时,它应该是那样的,如何解决这个问题. (3认同)

Chr*_*ris 33

这是我如何做到的.此技术负责将文本和详细文本标签适当地移动到左侧:

@interface SizableImageCell : UITableViewCell {}
@end
@implementation SizableImageCell
- (void)layoutSubviews {
    [super layoutSubviews];

    float desiredWidth = 80;
    float w=self.imageView.frame.size.width;
    if (w>desiredWidth) {
        float widthSub = w - desiredWidth;
        self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,desiredWidth,self.imageView.frame.size.height);
        self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x-widthSub,self.textLabel.frame.origin.y,self.textLabel.frame.size.width+widthSub,self.textLabel.frame.size.height);
        self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x-widthSub,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width+widthSub,self.detailTextLabel.frame.size.height);
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
}
@end

...

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = ...
    cell.detailTextLabel.text = ...
    cell.imageView.image = ...
    return cell;
}
Run Code Online (Sandbox Code Playgroud)


Rin*_*ain 21

图像视图作为子视图添加到tableview单元格

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 90, 70)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:8.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageWithData: imageData]];
[cell.contentView addSubview:imgView];
Run Code Online (Sandbox Code Playgroud)

  • 这有效.圆形的外观看起来很漂亮. (2认同)

Pet*_*ert 13

整个细胞不需要重新制作.您可以使用tableViewCells的indentationLevel和indentationWidth属性来移动单元格的内容.然后将自定义imageView添加到单元格的左侧.


Zai*_*han 7

一个简单的快速,

步骤1:创建UITableViewCell
步骤2的一个子类:将此方法添加到UITableViewCell的SubClass

override func layoutSubviews() {
    super.layoutSubviews()
    self.imageView?.frame = CGRectMake(0, 0, 10, 10)
}
Run Code Online (Sandbox Code Playgroud)

第3步:使用该子类创建单元格对象cellForRowAtIndexPath,

Ex: let customCell:CustomCell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)

第4步:享受


War*_*ior 5

更好地创建图像视图并将其作为子视图添加到单元格.然后您可以获得所需的帧大小.