Welcome, Guest
Username Password: Remember me

SCPropertyTypeSelection Screen
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: SCPropertyTypeSelection Screen

SCPropertyTypeSelection Screen 1 year, 8 months ago #1

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!
  • baberuth22
  • OFFLINE
  • Expert Boarder
  • Posts: 84
  • Karma: 2

Re: SCPropertyTypeSelection Screen 1 year, 8 months ago #2

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;
...
 
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72

Re: SCPropertyTypeSelection Screen 1 year, 8 months ago #3

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;
}
 
 
  • baberuth22
  • OFFLINE
  • Expert Boarder
  • Posts: 84
  • Karma: 2

Re: SCPropertyTypeSelection Screen 1 year, 8 months ago #4

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.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72

Re: SCPropertyTypeSelection Screen 1 year, 8 months ago #5

Thanks a lot. Worked like a charm.
  • baberuth22
  • OFFLINE
  • Expert Boarder
  • Posts: 84
  • Karma: 2
  • Page:
  • 1
Time to create page: 1.20 seconds