Вопрос

Я имею дело со старым проектом iPhone OS 2.x, и я хочу сохранить совместимость при разработке для 3.x.

Я использую NSInvocation, это код, подобный этому

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];

иметь код, который нравится как 3.0, так и 2.0, каждый из которых использует свой правильный синтаксис.

У меня возникла проблема на строке, которую я пометил вопросительными знаками.

Проблема заключается в том, что я пытаюсь присвоить arg2 константу, которая не была определена в OS 2.0.Поскольку все, что связано с NSInvocation, делается косвенно, чтобы избежать ошибок компилятора, как мне установить эту константу в переменную косвенным способом?Какой-то performSelector "присваивает значение переменной"...

возможно ли это?спасибо за любую помощь.

Это было полезно?

Решение

UITableViewCellStyleDefault определяется как 0 таким образом, вы можете использовать 0 везде, где вы обычно используете UITableViewCellStyleDefault.Кроме того, нет необходимости использовать NSInvocation, этого будет достаточно:

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:] все равно будет работать на 3.x, просто он устарел.

Другие советы

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  
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top