Challenge section


#1

Hi there,

my idea was to create only once random possessions. I know to make the challenge easier we should use two.

Here is my code for this version, it works and seems to have no errors, i hope.

The initializtion method

-(id)init
{
	//Call the superclass's designated initializer
	[super initWithStyle:UITableViewStyleGrouped];
	
	//Create an array of 10 random possesion objects
	possesions = [[NSMutableArray alloc] init];
        for (int i = 0; i < 10; i++) {
		[possesions addObject:[Possesion randomPossesion]];
	}

        //Create the arrays for the low worth and high worth possessions
	lowPossesions = [[NSMutableArray alloc]init];
	highPossesions = [[NSMutableArray alloc] init];	
	for (int i = 0; i < 10; i++) {
		Possesion *p = [possesions objectAtIndex:i];
		if ([p valueInDollars]< 50) {	
			[lowPossesions addObject:p];
		}
		else {
			[highPossesions addObject:p];
		}

	}

        //We no longer need possesions, therefore it can be released
	[possesions release];
	possesions =  nil;

	return self;
}

Set the number of sections to 2.
Set the header of the sections.
Get the number of rows for each section from the lowPossesion and highPossesion arrays.

and for inserting the data i used following code

//Asks the data source for a cell to insert in a particular location of the table view.
-(UITableViewCell *)tableView: (UITableView *)tv
		cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    ...

        //Set the text on the cell with the description of the low or highPossesion
	//that is at the nth index of low or highPossesions, where n = row this cell
	//will appear in on the tableview in that-appropriate section 
		if ([indexPath section]==0) {

			Possesion *p = [lowPossesions objectAtIndex:[indexPath row]];
					[[cell textLabel] setText:[p description]];
					NSLog(@"%@", [p description]);
			
		}
		else {
			Possesion *p = [highPossesions objectAtIndex:[indexPath row]];
					[[cell textLabel] setText:[p description]];
			NSLog(@"%@", [p description]);
			
				
		}
return cell;

}

In my opinion, this way is not more difficult than the suggested way.

If there are some memory mistakes or others i didn’t recognize, please write me.