Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a UITableView setup on my View Controller using 1 prototype cell. When the view controller loads it queries my database (code removed for security reasons) uses CustomisationObject to create a new object and then stores that into an array.

When I run the app the 4 cells that are loaded have the correct values that are stored in localArray.

enter image description here

When I scroll down to see the remaining two cells, they are not been loaded, there values are blank.

enter image description here

Custom cell class

CustomisationCell.h

#import <UIKit/UIKit.h>

@interface CustomisationCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *titleOutlet;
@property (weak, nonatomic) IBOutlet UISlider *sliderOutlet;

@end

CustomisationCell.m

#import "CustomisationCell.h"

@implementation CustomisationCell
@synthesize titleOutlet, sliderOutlet;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Object Class

CustomisationObject.h

@interface CustomisationObject : NSObject

@property (nonatomic, weak) NSString *localChannel;
@property (nonatomic, weak) NSString *localValue;

@end

View Controller Class

CustomisationViewController.h

#import <UIKit/UIKit.h>

@interface CustomisationViewController : UIViewController <UITableViewDelegate>

@end

CustomisationViewController.m

#import "CustomisationViewController.h"
#import "CustomisationCell.h"
#import "CustomisationObject.h"

@interface CustomisationViewController ()

@end

@implementation CustomisationViewController
NSMutableArray *localArray;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // SQL Code removed - query is return into variable resultSet

    localArray = [[NSMutableArray alloc] init];

    while([resultSet next])
    {   
        CustomisationObject *customisationObject = [[CustomisationObject alloc] init];
        customisationObject.localChannel = [resultSet stringForColumn:@"COLUMN_ONE"];
        customisationObject.localValue = [resultSet stringForColumn:@"COLUMN_TWO"];
        [localArray addObject:customisationObject];
    }
    [database close];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [localArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomisationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.titleOutlet.text = [[localArray objectAtIndex:[indexPath row]] localChannel];
    cell.sliderOutlet.value = [[[localArray objectAtIndex:[indexPath row]] localValue] intValue];

    return cell;
}

@end

If I change the height of the table view so all 6 cells fit on (no scrolling needed) they all load as expected. It only seems to be when the cell comes in and out of play.

share|improve this question
2  
Have you added a breakpoint in cellForRowAtIndexPath to see what the little rascal is up to? I suspect you'll find dequeueReusableCellWithIdentifier is returning nil, which is normal. –  trojanfoe Apr 28 at 13:47
    
Also make sure localArray contains all the objects you expect it to contain - again breakpoint and look at the contents of this object. –  petert Apr 28 at 13:53
    
Printing description of cell: <CustomisationCell: 0x1191aff0; baseClass = UITableViewCell; frame = (0 35; 320 62); autoresize = W; layer = <CALayer: 0x9f73e60>> –  user2920762 Apr 28 at 13:54
    
Cell does not seem to be returning nil. I've looped through localArray in viewDidLoad and all the objects are there. –  user2920762 Apr 28 at 13:55
    
If I change the height of the table view so all 6 cells fit on (no scrolling needed) they all load as expected. It only seems to be when the cell comes in and out of play. –  user2920762 Apr 28 at 14:05

1 Answer 1

All sorted. It turns out the memory properties needed to be copy and not weak in CustomisationObject.h.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.