0

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.

user2920762
  • 223
  • 1
  • 8
  • 17
  • 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 '14 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 '14 at 13:53
  • Printing description of cell: > – user2920762 Apr 28 '14 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 '14 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 '14 at 14:05
  • I haven't used this new way of populating cell but this link mentions about registerNib:forCellReuseIdentifier: to use the method properly. http://stackoverflow.com/questions/13174972/setting-style-of-uitableviewcell-when-using-ios-6-uitableview-dequeuereusablecel Hope this helps you – HMHero Apr 28 '14 at 14:05
  • Can you share the implementation of `CustomisationCell`? – Hermann Klecker Apr 28 '14 at 14:07
  • Added CustomisationCell.m implementation to question – user2920762 Apr 28 '14 at 14:12

1 Answers1

0

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

user2920762
  • 223
  • 1
  • 8
  • 17