Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom UITableViewCell with a UIImageView not loading image for certain cells
    primarykey
    data
    text
    <p>I have this really bizarre problem.</p> <p>Basically I have a UITableViewCell subclass that ive written and linked some outlets to a Nib. </p> <p>subclass of Cell.h</p> <pre><code>@property (weak, nonatomic) IBOutlet UIImageView *myImageView; </code></pre> <p>subclass of Cell.m</p> <pre><code>@synthesize myImageView = _myImageView; // do i need this? - (void) setUpCell... image:(UIImage*)image { if(image) self.myImageView.image = image; else self.myImageView.image = nil; } </code></pre> <p>In the view controller which manages the table view for the cell,</p> <p>I have registered with the Nib using the following code:</p> <pre><code>[self.tableView registerNib:[UINib nibWithNibName:pathToNibForBaseMenuCell bundle:nil] forCellReuseIdentifier:identifierForBaseMenuCell]; </code></pre> <p>In cellForRowAtIndexPath, I dequeue the cell and set the image...</p> <pre><code>CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell]; switch (indexPath.section) { case 2: [cell setUpCell...image:self.cogWheelImageForSettingsIcon]; break; default: break; } return cell; </code></pre> <p>}</p> <p>The strange thing is, say I have 10 cells in a section 2 (as above), and I set them up in an identical way, some cells show the cogwheel icon, and some don't! Scrolling those cells in and out of view seems to change which cells show the icon and which don't.</p> <p>This means that the registering the cell, including setting the re-use identifier, setting the subclass in the nib file, all of those sorts of things are fine (the common pitfalls).</p> <p>This pattern of behaviour seems to me to be memory related - as if something is being deallocated somewhere...</p> <p>Here is a picture to illustrate what I mean:</p> <p><img src="https://i.stack.imgur.com/6GZeN.png" alt="enter image description here"></p> <p>Please help! </p> <p>Thanks</p> <p>Vb</p> <p>Edit:</p> <p>OK just to be clear, im not using any data yet with this table, but in spite of that there's no reason why I those cogwheels shouldn't be appearing!</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0: return 1; break; case 1: return 10; break; case 2: return 10; break; default: return 0; break; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell]; switch (indexPath.section) { case 0: [cell setUpCell... text:@"Username" image:nil]; break; case 1: [cell setUpCell... text:@"Magazine" image:nil]; break; case 2: { NSString* str = [NSString stringWithFormat:@"Settings: %d",indexPath.row]; [cell setUpCell... text:str image:self.cogWheelImageForSettingsIcon]; break; } default: break; } return cell; } </code></pre> <p>The output of that is...</p> <p><img src="https://i.stack.imgur.com/cpC9F.png" alt="enter image description here"></p> <p>My point is that if im setting the same image for all cells which are in the 2nd section 'settings', why do only some of them have the image showing?</p> <p>The Custom Cell class:</p> <pre><code>@interface CustomCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView; @property (weak, nonatomic) IBOutlet UILabel *mainLabel; @property (weak, nonatomic) IBOutlet UILabel *arrowView; @property (weak, nonatomic) IBOutlet UIImageView *myImageView; - (void) setUpCelltext:(NSString*)text image:(UIImage*)image; @implementation CustomCell - (void) setUpCelltext:(NSString*)text image:(UIImage*)image { self.backgroundImageView.image = [UIImage imageNamed:pathForBackgroundImage]; self.myImageView.layer.cornerRadius = kCornerRadiusOfImageView; self.myImageView.layer.masksToBounds = YES; if(image) self.myImageView.image = image; else self.myImageView.image = nil; NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:self.fontForText forKey:NSFontAttributeName]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary]; self.mainLabel.attributedText = attrString; } @end </code></pre> <p>The cells have custom fonts, an arrow view, a background view - they all work as they should! They change depending on section. Its just the image that doesn't work. You can see that this function called by cellForRowAtIndexPath will either set an image, if its provided, or set the imageview to nil if it isn't. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload