title: How to set the table view cell accessory view to retain a previously initialized UIImageView?
body: <p>Let's say I have a property in my view controller, defined as follows:</p>
  
  <pre><code>@property (nonatomic, retain) UIImageView *checkmarkOffAccessoryView;
  </code></pre>
  
  <p>I <code>@synthesize</code> this in the implementation, <code>release</code> it in <code>-dealloc</code> and initialize it in <code>-viewDidLoad</code> as follows:</p>
  
  <pre><code>self.checkmarkOffAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]] autorelease];
  </code></pre>
  
  <p>So far so good.</p>
  
  <p>When I use it in my table view delegate as an accessory view for multiple cells, two things happen:</p>
  
  <ol>
  <li>Only one cell's accessory view shows the image</li>
  <li>The application UI freezes. </li>
  </ol>
  
  <p>The app doesn't crash, as near as I can tell, the UI simply becomes unresponsive. This is both in the simulator and on the device.</p>
  
  <p>Here is how I use the initialized property with my cell:</p>
  
  <pre><code>- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      // initialize or dequeue cell...
  
      if (condition)
          cell.accessoryView = self.checkmarkOffAccessoryView;
      else
          cell.accessoryView = nil;
  }
  </code></pre>
  
  <p>With the aforementioned code, only one cell shows the accessory view and the UI freezes.</p>
  
  <p>If I initialize the <code>UIImageView</code> instance directly in the delegate method I get all condition-satisfying cells showing the accessory view and I do not experience the UI freeze:</p>
  
  <pre><code>- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      // initialize or dequeue cell...
  
      if (condition)
          cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]] autorelease];
      else
          cell.accessoryView = nil;
  }
  </code></pre>
  
  <p>My goal is to initialize as few objects as possible and reuse one <code>UIImageView</code>. I'm curious why the first chunk of code is problematic and what I could do to fix this. </p>
  
  <p>It seems like the cell's <code>accessoryView</code> property should just increment the <code>retain</code> count of <code>self.checkmarkOffAccessoryView</code> but it appears I am missing some detail. </p>
  
  <p>What have I overlooked? Thanks for your advice.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I think that:</p>
  
  <pre><code>self.checkmarkOffAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]] autorelease];
  </code></pre>
  
  <p>is the same as:</p>
  
  <pre><code>UIImageView *uncheckedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]];
  self.checkmarkOffAccessoryView = uncheckedView;
  [uncheckedView release];
  </code></pre>
  
  <p>Either way, I experience the same freeze symptom.</p>