Abdullah Saeed Abdullah Saeed - 2 years ago 115
Swift Question

Images not being set programmatically in KDCircularProgress

I am trying to set an image inside KDCircularProgress which is placed inside a UITableViewCell.

enter image description here

When I add an image from StoryBoard it successfully shows, but when I try to add it programatically the image does not appear.

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 is the UIImageView outlet in UITableViewCell class and I am trying to set the dynamic image in table view cell in the View Controller class in which the TableView delegate methods are implemented.

What am I doing wrong?

Answer Source

I'm not sure what DashBoardCell looks like so this may be wrong, but it appears that dImage is never added to the view hierarchy. If the DashBoardCell has already created dynamicImageView in your storyboard, and I don't see why it wouldn't, then you don't need to worry about creating a UIImageView, you could simply set the image like

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

If it hasn't, then you'll want to add it with something like


Or if DashBoardCell's dynamicImageView has a didSet observer you could insert the new view there. You'll also want to make sure you override prepareForReuse in DashBoardCell to remove dynamicImageView since you'll be creating a new one in cellForRowAtIndexPath.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download