2016-04-25 41 views
1

我試圖在放置在UITableViewCell中的KDCircularProgress中設置一個圖像。未在KDCircularProgress中以編程方式設置圖像

enter image description here

當我從故事板中添加圖像成功地顯示,但是當我嘗試添加它編程的圖像不會出現。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cellToReturn: DashBoardCell! = tableView .dequeueReusableCellWithIdentifier("DashBoardCell") as? DashBoardCell 
    if (cellToReturn == nil) { 
     cellToReturn = DashBoardCell(style: UITableViewCellStyle.Default, reuseIdentifier:"DashBoardCell") 
    } 
    let image: UIImage = UIImage(named: "ImageName")! 
    dImage = UIImageView(image: image) 
    dImage!.frame = CGRectMake(0,0,30,25) 
    dImage?.contentMode = UIViewContentMode.ScaleToFill 
    cellToReturn.dynamicImageView = dImage 

dynamicImageView是在UITableViewCell類,所述的UIImageView出口,我試圖設置在表視圖細胞的動態圖像中,其中所述的TableView委託方法被實現的視圖控制器類。

我在做什麼錯?

回答

1

我不確定DashBoardCell是什麼樣子,所以這可能是錯誤的,但似乎dImage永遠不會被添加到視圖層次結構。如果DashBoardCell在你的故事板已經創建dynamicImageView,我不明白爲什麼它不會,那麼你就不需要擔心創建一個UIImageView,你可以簡單地設置圖像像

cellToReturn.dynamicImageView.image = UIImage(named: "ImageName") 

如果還沒有,那麼你要的東西添加它像

cellToReturn.contentView.addSubview(dImage) 

或者如果DashBoardCelldynamicImageViewdidSet觀察者,你可以插入新的觀點有。您還需要確保您覆蓋DashBoardCell中的prepareForReuse以刪除dynamicImageView,因爲您將在cellForRowAtIndexPath中創建一個新的。

+0

謝謝!你的第一個解決方案奏效 –

相關問題