UA-17470720-3

Jump to content


Photo
- - - - -

SCPropertyTypeSelection Screen


  • Please log in to reply
4 replies to this topic

#1 Bryan Saltzman

Bryan Saltzman

    Experienced Member

  • STV 2.0
  • PipPipPipPip
  • 70 posts
Reputation: 0
Neutral

Posted 30 August 2010 - 09:16 AM

I want to use the SCPropertyTypeSelection to allow the user to select a named color.
The property type selection table should be a list of colors with each row containing a small imageview filled by that row's color.

Code would be similar to:

NSArray *colorArray = [[NSArray alloc] initWithObjects:@"Blue", @"Brown", ..., nil];

SCPropertyDefinition *colorPropertyDef = [objectClassDef propertyDefinitionWithName:@"color"];
	designerPropertyDef.type = SCPropertyTypeSelection;
	designerPropertyDef.attributes = [SCSelectionAttributes attributesWithItems:colorArray allowMultipleSelection:NO allowNoSelection:YES];
	designerPropertyDef.title = @"Color";

// Add image code ??




How would I add the image this property selection view?

Thanks a lot!

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 30 August 2010 - 11:31 AM

You can easily add images to your selection by using the imageViewArray property of SCSelectionAttributes:

...
designerPropertyDef.attributes = [SCSelectionAttributes attributesWithItems:colorArray allowMultipleSelection:NO allowNoSelection:YES];
designerPropertyDef.attributes.imageViewArray = myImageViewArray;
...


#3 Bryan Saltzman

Bryan Saltzman

    Experienced Member

  • STV 2.0
  • PipPipPipPip
  • 70 posts
Reputation: 0
Neutral

Posted 31 August 2010 - 12:17 PM

I tried using your code as below (there is extra code because of debugging attempts:

 NSArray *colors = [NSArray arrayWithObjects:@"White",@"Black", @"Dark Grey", @"Light Grey", @"Gray", @"Red", @"Green", @"Blue", @"Cyan", @"Yellow",
					   @"Magenta", @"Orange", @"Purple", @"Brown",nil];
   

SCPropertyDefinition *colorPropertyDef = [shoeClassDef propertyDefinitionWithName:@"colorIndex"];
	colorPropertyDef.type = SCPropertyTypeSelection;
	colorPropertyDef.attributes = [SCSelectionAttributes attributesWithItems:colors allowMultipleSelection:NO allowNoSelection:YES];
	colorPropertyDef.title = @"Color";
	
	NSMutableArray *colorImageArray = [[NSMutableArray alloc] initWithCapacity:[colors count]];
	float sizeOfImage = 44.0f;
	
	for( NSString *thisColor in colors)
	{
		UIImage *colorImage = [[UIImage alloc] init];
		if( [thisColor isEqualToString:@"White"]){
			colorImage = [ImageManipulator imageWithColor:[UIColor whiteColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Black"){
			colorImage = [ImageManipulator imageWithColor:[UIColor blackColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Dark Grey"){
			colorImage = [ImageManipulator imageWithColor:[UIColor darkGrayColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Light Gray"){
			colorImage = [ImageManipulator imageWithColor:[UIColor lightGrayColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Gray"){
			colorImage = [ImageManipulator imageWithColor:[UIColor grayColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Red"){
			colorImage = [ImageManipulator imageWithColor:[UIColor redColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Green"){
			colorImage = [ImageManipulator imageWithColor:[UIColor greenColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Blue"){
			colorImage = [ImageManipulator imageWithColor:[UIColor blueColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Cyan"){
			colorImage = [ImageManipulator imageWithColor:[UIColor cyanColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Yellow"){
			colorImage = [ImageManipulator imageWithColor:[UIColor yellowColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Magenta"){
			colorImage = [ImageManipulator imageWithColor:[UIColor magentaColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Orange"){
			colorImage = [ImageManipulator imageWithColor:[UIColor orangeColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Purple"){
			colorImage = [ImageManipulator imageWithColor:[UIColor purpleColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}else if( thisColor == @"Brown"){
			colorImage = [ImageManipulator imageWithColor:[UIColor brownColor] sizeX:sizeOfImage sizeY:sizeOfImage];
		}
		UIImageView *colorImageView = [[UIImageView alloc] initWithImage:colorImage];
		colorImageView.frame = CGRectMake(0, 0, sizeOfImage, sizeOfImage);
		[colorImage release];
		[colorImageArray addObject:colorImageView];
		[colorImageView release];
	}
	NSArray *imageViewArray = [NSArray arrayWithArray:colorImageArray];
	colorPropertyDef.attributes.imageViewArray = imageViewArray;
	


other methods

+ (UIImage *)imageWithColor:(UIColor *)color sizeX:(float)sizeX sizeY:(float)sizeY{
	CGRect rect = CGRectMake(0.0f, 0.0f, sizeX, sizeY);
	UIGraphicsBeginImageContext(rect.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGContextSetFillColorWithColor(context, [color CGColor]);
	CGContextFillRect(context, rect);
	
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return image;
}




But I receive a EXC_BAD_ACCESS error in the following block of SCTableViewModel.m :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	SCTableViewCell *cell = [self cellAtIndexPath:indexPath];
	if([cell.delegate conformsToProtocol:@protocol(SCTableViewCellDelegate)]
	   && [cell.delegate respondsToSelector:@selector(willConfigureCell:)])
	{
		[cell.delegate willConfigureCell:cell];
	}
	else
		if([self.delegate conformsToProtocol:@protocol(SCTableViewModelDelegate)]
		   && [self.delegate 
			   respondsToSelector:@selector(tableViewModel:willConfigureCell:forRowAtIndexPath:)])
		{
			[self.delegate tableViewModel:self willConfigureCell:cell forRowAtIndexPath:indexPath];
		}
	
	CGFloat cellHeight = cell.height;
	// Check if the cell has an image in its section and resize accordingly
	SCTableViewSection *section = [self sectionAtIndex:indexPath.section];
	if([section.cellsImageViews count] > indexPath.row)
	{
		UIImageView *imageView = [section.cellsImageViews objectAtIndex:indexPath.row];
		if([imageView isKindOfClass:[UIImageView class]])
		{
			if(imageView == nil){
				NSLog(@"Image was nil");
				cellHeight = 44.0f;
			}else{
/////////////////////////  ERROR at LINE BELOW  // I think imageView has no size??
				if(cellHeight < imageView.image.size.height)
					cellHeight = imageView.image.size.height;
			}
		}
	}
	
	return cellHeight;
}



#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 31 August 2010 - 01:45 PM

1- This is just FYI, and has nothing to do with the problem: By assigning

UIImage *colorImage = [[UIImage alloc] init];

and then reassigning it by

colorImage = [ImageManipulator imageWithColor:[UIColor whiteColor] sizeX:sizeOfImage sizeY:sizeOfImage];

you're actually leaking memory since the first [UIImage alloc] have not been released.


2- Now to your problem: I think your problem is because you're not retaining the autoreleased image value coming from the imageWithColor method. As soon as the "if" block terminates, the autorelease image object is released, thus leaving colorImage pointing to deallocated memory.

#5 Bryan Saltzman

Bryan Saltzman

    Experienced Member

  • STV 2.0
  • PipPipPipPip
  • 70 posts
Reputation: 0
Neutral

Posted 01 September 2010 - 01:33 PM

Thanks a lot. Worked like a charm.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users