【问题标题】:How to resize the imageView?如何调整图像视图的大小?
【发布时间】:2018-07-07 19:01:36
【问题描述】:

如何调整 imageView 的大小? Xcode 说 imageView 的初始化从未使用过。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    let city = nicosiaPlaces [indexPath.row]
    cell?.textLabel?.text = city
    let imageView = CGSize(width: 100, height: 100)
    cell?.imageView?.image = UIImage(named: city)
    return cell!
}

【问题讨论】:

    标签: swift imageview


    【解决方案1】:

    您只需在每次调用该方法时创建一个新的 CGSize,您应该改为更改单元类中的大小。如果您使用带有 AutoLayout 的情节提要,您应该更改情节提要中的大小。

    【讨论】:

      【解决方案2】:

      我猜你的问题里面有一些缺陷。

      假设您需要调整 Imageview 的大小,我正在为您发布小片段。

      永远不要在func tableView(cellForRowAt:IndexPath) 中初始化任何新组件 因为每次它都会创建一个新实例,直到它们被正确处理。 您已将引用作为 let imageView = CGSize(--,--),但您再次从 Cell 方法调用实例。

      不要那样做。

        override func awakeFromNib(){
            let imageView = UIImageView(CGRectMake(0,0,150,150))
            imageView.contentMode = .scaleAspectFill
            contentView.addSubView(imageView)
      }
      

      现在在 Class 方法中获取 ImageView 的引用并根据需要调整大小。

            overrride  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
            {
               let cell = tableView.deququeResuableCell(withIdentifier: "cellID",forIndexPath: indexPath) as! yourCustomCellClassName
                cell.imageView?.image = UIImage(named: "yourImage")
             // If you need to resize the Image make changes in Frame of Image or ContentMode of the Image.
               return cell
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        相关资源
        最近更新 更多