Question

I am dealing with an old iPhone OS 2.x project and I want to keep compatibility, while designing for 3.x.

I am using NSInvocation, is a code like this

NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
       [cell methodSignatureForSelector:
                                    @selector(initWithStyle:reuseIdentifier:)]];
[invoc setTarget:cell];
[invoc setSelector:@selector(initWithStyle:reuseIdentifier:)];
int arg2 = UITableViewCellStyleDefault;  //????
[invoc setArgument:&arg2 atIndex:2];
[invoc setArgument:&identificadorNormal atIndex:3];
[invoc invoke];

to have a code in a way that both 3.0 and 2.0 like, each one using its proper syntax.

I am having a problem on the line I marked with question marks.

The problem there is that I am trying to assign to arg2, a constant that has not been defined in OS 2.0. As everything with NSInvocation is to do stuff indirectly to avoid compiler errors, how do I set this constant to a variable in an indirect way? Some sort of performSelector "assign value to variable"...

is that possible? thanks for any help.

Was it helpful?

Solution

UITableViewCellStyleDefault is defined as 0 so you can use 0 wherever you would normally use UITableViewCellStyleDefault. Also, there is no need to use an NSInvocation, this will do:

UITableViewCell *cell = [UITableViewCell alloc];
if ([cell respondsToSelector:@selector(initWithStyle:reuseIdentifier:)])
    cell = [(id)cell initWithStyle:0 reuseIdentifier:reuseIdentifier];
else
    cell = [cell initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier];

-[UITableViewCell initWithFrame:reuseIdentifier:] will still work on 3.x, it's just deprecated.

OTHER TIPS

NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
       [cell methodSignatureForSelector:
                                    @selector(initWithStyle:reuseIdentifier:)]];
[invoc setTarget:cell];
[invoc setSelector:@selector(initWithStyle:reuseIdentifier:)];

int arg2;

#if (__IPHONE_3_0)
arg2 = UITableViewCellStyleDefault;
#else
//add 2.0 related constant here
#endif  

[invoc setArgument:&arg2 atIndex:2];
[invoc setArgument:&identificadorNormal atIndex:3];
[invoc invoke];


#if (__IPHONE_3_0)
arg2 = UITableViewCellStyleDefault;
#endif  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top