简体   繁体   中英

Trouble Loading Images in UITableView

I'm trying to put images into my UITableView . The Table is set up with a custom subclass cell. In the Subclass I have outlets:

@IBOutlet var titleLabel: UILabel!

`@IBOutlet var pillarIcon: UIImageView!`

In the Superclass I created a NSMutableArray for both:

var objects: NSMutableArray! = NSMutableArray()

var imageObjects: NSMutableArray! = NSMutableArray()

I place things into the Arrays within the viewDidLoad Method Each array has 4 items.

So when I call the Items:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.objects.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

    cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String

    cell.pillarIcon.image = self.imageObjects.objectAtIndex(indexPath.row) as? UIImage


    return cell


}

The cell.titleLabel.text items come up but the images in the cell.pillarIcon.image do not come show up in the table.

I have been working on this for a couple hours now and am going in circles it feels like. Also the image files have been loaded into the main file so that is not the problem.

Thanks in advance.

Make sure that your images are loaded into your Images.xcassets and then use the UIImage(name:) method to load the images.


Currently, you are loading your images into your imageObjects array as follows:

self.imageObjects.addObject("book.jpg")

You are not using the UIImage to load the image.

Once your images are a part of your project's image assets ( Images.xcassets ), load your images like so:

self.imageObjects.addObject(UIImage(named: "book"))

(The above example assumes that book is the name of the image asset group.)

Note, this Adding Image Assets web page has instructions on how to set up an image asset group.

It seems like you are loading images incorrectly, like @whyceewhite said in his answer.

I will try to make it clear to you:

  1. open Assets.xcassets

xcassets

  1. Drop all images you want to use under AppIcon xcassets2

  2. Add images to your array this way:

imageObjects.addObject(UIImage(named: "whiteboardapp")!)

Use the same name displayed on Assets.xcassets folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM