سؤال

أنا أتعامل مع مشروع 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 هو القيام بالأشياء بشكل غير مباشر لتجنب أخطاء المحول البرمجي، كيف يمكنني تعيين هذا ثابتا للمتغير بطريقة غير مباشرة؟ نوع من industriense "تعيين قيمة إلى المتغير" ...

هل هذا ممكن؟ شكرا على اي مساعدة.

هل كانت مفيدة؟

المحلول

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