Edit a cell in the selected row

Apparently I've been working with bindings for too long because I can't figure out how to do it. I have a table with multiple columns. When a row is selected, you can change its priority, which changes the master data attribute. I also set this as IBAction. Basically I want to access the Core Data attribute value from my code. Then I want to set the first column of any row (and change my priority) to multiple exclamation marks corresponding to the priority level.

Sorry, this is set out in confusion; here's an example:

Row 7 is selected. I change my priority to 2. The Core Data attribute myPriority is now set to 2. The code block now runs. It gets the priority of the selected row (row 7) of the Core Data form and wants to set column 1 of the selected row (row 7) to 2 exclamation marks (priority 2).

Thanks!

0


a source to share


1 answer


If you are used to bindings, I would recommend looking at NSValueTransformer; in particular, subclassing something that converts the precedence value to an exclamation mark string. Then you simply specify the name (the same as used in +setValueTransformer:forName:

) as the "value converter" attribute in your bindings.

For example, the code would look like this:

@interface PriorityTransformer : NSValueTransformer
@end

@implementation PriorityTransformer
+ (Class) transformedValueClass { return ( [NSString class] ); }
+ (BOOL) allowsReverseTransformation { return ( NO ); }
- (id) transformedValue: (id) value
{
    // this makes the string creation a bit simpler
    static unichar chars[MAX_PRIORITY_VALUE] = { 0 };
    if ( chars[0] == 0 )
    {
        // ideally you'd use a spinlock or such to ensure it setup before 
        //  another thread uses it
        int i;
        for ( i = 0; i < MAX_PRIORITY_VALUE; i++ )
            chars[i] = (unichar) '!';
    }

    return ( [NSString stringWithCharacters: chars
                                     length: [value unsignedIntegerValue]] );
}
@end

      



Then you put this code in the same file as the central class (for example, the application delegate) and register it with this method +initialize

to ensure that it is detected early enough for any pens to find it:

+ (void) initialize
{
    // +initialize is called for each class in a hierarchy, so always
    //  make sure you're being called for your *own* class, not some sub- or
    //  super-class which doesn't have its own implementation of this method
    if ( self != [MyClass class] )
        return;

    PriorityTransformer * obj = [[PriorityTransformer alloc] init];
    [NSValueTransformer setValueTransformer: obj forName: @"PriorityTransformer"];
    [obj release];   // obj is retained by the transformer lookup table
}

      

+1


a source







All Articles