title: Setting images in UITableViewCell in Swift
body: <p>I have a list of reddit posts that I want to display the thumbnail of, if it exists.  I have it functioning, but it's very buggy.  There are 2 main issues:</p>
  
  <ul>
  <li>Images resize on tap</li>
  <li>Images shuffle on scroll</li>
  </ul>
  
  <p>This is the code:</p>
  
  <pre><code>override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Post", forIndexPath: indexPath) as UITableViewCell
      let post = swarm.posts[indexPath.row]
      cell.textLabel!.text = post.title
  
      if(post.thumb? != nil &amp;&amp; post.thumb! != "self") {
          cell.imageView!.image = UIImage(named: "first.imageset")
          var image = self.imageCache[post.thumb!]
  
          if(image == nil) {
              FetchAsync(url: post.thumb!) { data in // code is at bottom, this just drys things up
                  if(data? != nil) {
                      image = UIImage(data: data!)
                      self.imageCache[post.thumb!] = image
                      dispatch_async(dispatch_get_main_queue(), {
                          if let originalCell = tableView.cellForRowAtIndexPath(indexPath) {
                              originalCell.imageView?.image = image
                              originalCell.imageView?.frame = CGRectMake(5,5,35,35)
                          }
                      })
                  }
              }
          } else {
              dispatch_async(dispatch_get_main_queue(), {
                  if let originalCell = tableView.cellForRowAtIndexPath(indexPath) {
                      originalCell.imageView?.image = image
                      originalCell.imageView?.frame = CGRectMake(5,5,35,35)
                  }
              })
          }
      }
  
      return cell
  }
  </code></pre>
  
  <p>This is the app when it loads up - looks like everything is working:</p>
  
  <p><img src="https://i.stack.imgur.com/18mQe.png" alt="loaded up, looks like everything is working"></p>
  
  <p>Then if I tap on an image (even when you scroll) it resizes:</p>
  
  <p><img src="https://i.stack.imgur.com/MLWrU.png" alt="pictures resize on tap"></p>
  
  <p>And if you scroll up and down, the pictures get all screwy (look at the middle post - Generics fun):</p>
  
  <p><img src="https://i.stack.imgur.com/T6SCY.png" alt="pictures shuffle on scroll"></p>
  
  <p>What am I doing wrong?</p>
  
  <p>** Pictures and Titles are pulled from reddit, not generated by me **</p>
  
  <hr>
  
  <p>EDIT: FetchAsync class as promised:</p>
  
  <pre><code>class FetchAsync {
      var url: String
      var callback: (NSData?) -&gt; ()
  
      init(url: String, callback: (NSData?) -&gt; ()) {
          self.url = url
          self.callback = callback
          self.fetch()
      }
  
      func fetch() {
          var imageRequest: NSURLRequest = NSURLRequest(URL: NSURL(string: self.url)!)
          NSURLConnection.sendAsynchronousRequest(imageRequest,
              queue: NSOperationQueue.mainQueue(),
              completionHandler: { response, data, error in
                  if(error == nil) {
                      self.callback(data)
                  } else {
                      self.callback(nil)
                  }
          })
          callback(nil)
      }
  }
  </code></pre>