【问题标题】:How to set fixed size for UITableViewCell's ImageView?如何为 UITableViewCell 的 ImageView 设置固定大小?
【发布时间】:2013-01-25 10:24:44
【问题描述】:

对于我的应用程序的联系人视图,我在表格视图中添加了基于他的电子邮件 ID 的个人用户图像。由于每个用户的图像大小不同,图像视图并非对所有用户都是唯一的,有些图像视图的宽度更大,所以我将 UItableViewCell 的帧大小设置为固定宽度,但宽度大小仍然不同。

 [cell.imageView setFrame:CGRectMake(0, 0, 55, 55)];

我该怎么办?我需要添加新的图像视图作为单元格的子视图吗?有什么想法吗?

【问题讨论】:

标签: iphone ios cocoa-touch uitableview uiimageview


【解决方案1】:

UITableView 的 imageView 属性是只读的。您可以在设置为单元格的 imageView 之前调整图像大小。要调整图像大小,您可以从StackOverflow answer 获得帮助。

另外,如果您从网络环境加载图像,您可以使用我的 AFNetworking 分支,我在 UIImageView 扩展中添加了图像大小调整选项。 https://github.com/polatolu/AFNetworking

【讨论】:

    【解决方案2】:

    您应该设置 imageview 的属性,以便以不同的大小显示不同的图像。

    例如,

    <imageview_name>.contentMode = UIViewContentModeScaleAspectFit;
    

    享受编程!

    【讨论】:

      【解决方案3】:

      我正在使用相同的 JSON 文件来填充 UITableView,这是我在 CustomCell 中用于通用应用程序的内容:

      - (void)layoutSubviews {
          [super layoutSubviews];
          if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
              self.imageView.frame = CGRectMake(0,0,300,100);
      
          }else if  (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
              self.imageView.frame = CGRectMake(0,0,180,60);
          }
          float limgW =  self.imageView.image.size.width;
          if(limgW > 0) {
      
              if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
                  self.textLabel.frame = CGRectMake(320,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);
      
              }else if  (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
                  self.textLabel.frame = CGRectMake(182,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);
              }
      
          }
      }
      

      【讨论】:

        【解决方案4】:

        继承 UITableViewCell 并覆盖该方法;

        - (void)layoutSubviews {
        
            [super layoutSubviews];
        
            self.imageView.frame = CGRectMake(0,0,55,55);
        
            //Any additional setting that you want to do with image view
        
            [self.imageView setAutoresizingMask:UIViewAutoresizingNone];
        
        }
        

        【讨论】:

        • 这确实修复了图像大小,但不幸的是文本标签不会移动位置以与新图像大小对齐
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-20
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        相关资源
        最近更新 更多