Reusable Cell Bug when total number of rows >= 10


#1

While I was working through the challenges I was continuously messing around with the number of BNRItems that I would create. What I’ve noticed is that as long as I don’t exceed 9 I’m fine everything works as it should. Once I go passed that well that’s when things go wrong.

Setting the BNRItems to 10 and using the debugger I notice that after the 3rd row is generated no cells are generated. For the remaining rows the app reuses the first few cells from section one.

I’ve also notice that when I set it to say 19, the second section (still present) is only using reusing cells from the first section and not generating any cells for the second.

Any insight would be much appreciated.

ItemsViewController.m

#import "ItemsViewController.h"
#import "BNRItem.h"
#import "BNRItemStore.h"

@implementation ItemsViewController

- (id)init
{
	self = [super initWithStyle:UITableViewStyleGrouped ];
	
	if(self){
		for (int i = 0; i < 19; i++){
			[[BNRItemStore sharedStore] createItem];
						
		}
	}
	
	moreThan50 = [[NSMutableArray alloc] init];
	lessThan50 = [[NSMutableArray alloc] init];
	
	for (int i = 0; i <= ([[[BNRItemStore sharedStore] allItems] count] -1); i++){
		if ([[[[BNRItemStore sharedStore] allItems] objectAtIndex:i] valueInDollars] <= 50){
			[lessThan50 addObject:[[[BNRItemStore sharedStore] allItems] objectAtIndex:i]];
		} else {
			[moreThan50 addObject:[[[BNRItemStore sharedStore] allItems] objectAtIndex:i]];
		}
	}
	

	NSLog(@"%d", [moreThan50 count]);
	[moreThan50 addObject:@"No More Items"];
	NSLog(@"%d", [moreThan50 count]);
	
	for (int i = 0; i < [moreThan50 count]; i++){
		//NSLog(@"%d", [[lessThan50 objectAtIndex:i] valueInDollars]);
		NSLog(@"%@", [moreThan50 objectAtIndex:i]);
	}
	
	return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
	return [self init];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	int section = 2;
	return section;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	NSString *header;
	
	if (section == 0){
		header = @"Items less than $50";
	}
	
	if (section == 1){
		header = @"Items more than $50";
	}
	
	return header;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 	int x = 0;
	
	if (section == 0){
		x = [lessThan50 count];
				
	}
	
	if (section == 1){
		x = [moreThan50 count];
		NSLog(@"%d", x);
				
	}
	

	return x;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	float height = 0.0;
	
	if([indexPath row] == ([moreThan50 count]-1) && [indexPath section] == 1){
		height = 40.0;
	} else {
		height = 60.0;
	}
	
	return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
	
	/*if (!cell){
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
		
	}
	
	if ([indexPath section] == 0){
		BNRItem *p = [lessThan50 objectAtIndex:[indexPath row]];
		[[cell textLabel] setText:[p description]];
	} else if ([indexPath section] == 1) {
		BNRItem *p = [moreThan50 objectAtIndex:[indexPath row]];
		[[cell textLabel] setText:[p description]];
		}
	*/
	
	NSLog(@"%@", indexPath);
	
	UILabel *mainLabel;
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
	if (!cell){
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
		
		cell.accessoryType = UITableViewCellAccessoryNone;
		
		mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 60.0)];
		mainLabel.font = [UIFont systemFontOfSize:20.0];
		
		
		[cell.contentView addSubview:mainLabel];
		
		if ([indexPath section] == 0){
			BNRItem *p = [lessThan50 objectAtIndex:[indexPath row]];
			mainLabel.text = [p itemName];
		}
		
		if ([indexPath section] == 1) {
			
			if(!([indexPath row] == ([moreThan50 count]-1))){
				BNRItem *p = [moreThan50 objectAtIndex:[indexPath row]];
				mainLabel.text = [p description];
							 
			} else {
				NSString *s = [moreThan50 objectAtIndex:[indexPath row]];
				mainLabel.text = s;
				NSLog(@"%@", [moreThan50 objectAtIndex:[indexPath row]]);
				
			}
		}

	}
	
	return cell;
}
@end

#2

Well I figured it out. I was reviewing other people’s solutions to see what I could do to improve, or to just see how other people went about it, anyway I was cleaning out the custom view malarky when I noticed that all my cell conditions were inside the if(!cell){ } method. *DERP!