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.
When I scroll down to see the remaining two cells, they are not been loaded, there values are blank.
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.
cellForRowAtIndexPath
to see what the little rascal is up to? I suspect you'll finddequeueReusableCellWithIdentifier
is returningnil
, which is normal. – trojanfoe Apr 28 at 13:47localArray
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