2016-03-19 34 views
0

我在UIViewController中有一個UITableView,以及在我自己的自定義單元類中定義的1個原型單元。我在右上角UIImageView,具有頂部和後隨區間的限制,以及一個固定的寬度和高度:如何在自定義UITableViewCell中創建UIImageVIew? (Swift)

In Interface Builder

是,當我運行該程序時,將創建該問題的TableView但該imageView過大:

At runtime

研究本網站上的一些類似的問題後,我得出的結論是:

僅當您爲其分配 圖像時,單元格的imageView纔會顯示並被設定大小。

我已經試圖在自定義單元格類下面的代碼,以及cellForRowAtIndexPath功能:

cell.imageView?.frame = CGRectMake(300, 52, 292, 136) 
cell.imageView?.clipsToBounds = true 
cell.contentView.addSubview(cell.imageView!) 

我缺少的東西或我該怎麼辦,以獲得imageView的大小我」是否已經爲它的約束?

預先感謝您!

+0

是的,你需要添加約束細胞的含有觀點和沒有必要設置ImageView的框架編程。 例如:http://postimg.org/image/utqzbrjn1/ –

+0

你可以在storyboard中給你的tableviewCell截圖嗎?如果您向我們展示您的約束條件,它會有所幫助廣告將很高興在'cellForRowAtIndexPath'下顯示您的完整代碼。 –

回答

0

出於興趣,固定的寬度和高度約束是什麼值?由於視圖控制器的大小不同,很難區分界面生成器和設備屏幕截圖。

我的建議與固定寬度和高度的限制,而是做掉:

•引腳圖像視圖到電池的底部的底部 - 這給圖片查看它的高度。

•在圖像視圖中添加縱橫比約束 - 這會根據圖像的高度爲圖像視圖提供寬度。

這意味着即使最終改變了單元格的高度,圖像視圖也會始終適合單元格內部。

雖然使用原型單元格和UITableViewCell子類的榮譽 - 我完全同意這種方法!

+0

感謝您的快速回復馬修!我試過了你的建議,但它給了我相同的結果。寬度爲292,高度爲136.我應該提到該設備是iPhone 6. imageView是唯一的問題,它似乎是因爲它的約束沒有被應用。 – vblaga

+0

你可能提供更多代碼圍繞表視圖本身的設置,以及cellForRowAtIndexPath:方法?那麼我們可能會看到你是否在做一些你不應該做的事情,或者忘記做一些你應該做的事情! –

1

的UIView具有屬性contentMode - 儘量把它定義爲ScaleToFill:

self.someImage.contentMode = .ScaleToFill 
0

你的代碼看起來模糊,雖然我您在使用自定義單元格級的同意。這樣做是爲了嘗試下載所有圖像集,如果無法下載,它將用保存在資產上的圖像佔位符替換圖像。所有圖像在加載時都具有相同的尺寸。

以下是您可以做的工作。這是您的自定義課程。

class SomeCell: UITableViewCell { 

    @IBOutlet weak var imgMain: UIImageView! 
    @IBOutlet weak var lblMain: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
     imgMain.layer.cornerRadius = 5.0 
     imgMain.clipsToBounds = true 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

    func configureCell(image: UIImage, text: String){ 
     imgMain.image = image 
     lblMain.text = text 
    } 

} 

然後在您的視圖控制器

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    var somePics = ["http://www.something.com/pic1.jpg","http://www.something.net/pic2.jpg","http://www.something.com/pic3.jpg","http://www.something.com/pic4.jpg","http://www.something.net/pic5.jpg"] 

    var someTitles = ["Picture 1", "Picture 2", "Picture 3", "Picture 4", "Picture 5"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.delegate = self 
     tableView.dataSource = self 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     if let cell = tableView.dequeueReusableCellWithIdentifier("SomeCell") as? SomeCell { 

      var img: UIImage! 

      let url = NSURL(string: somePics[indexPath.row])! 

      if let data = NSData(contentsOfURL: url) { 
       img = UIImage(data: data) 
      }else { 
       img = UIImage(named: "somePlaceholderpic") 
      } 

      cell.configureCell(img, text: someTitles[indexPath.row]) 

      return cell 

     } else { 

      return SomeCell() 

     } 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return somePics.count 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

希望這有助於。 :)

1

感謝大家的幫助!

我犯了一個愚蠢的錯誤,引用了自定義單元的通用imageView?,而不是我用另一個名字製作的IB Outlet

錯誤:

cell.imageView?.image = UIImage(named: "whatever name") 

右:

cell.dogImageView.image = UIImage(named: "labrador") 
相關問題