iPhone - 如何使用带复杂参数的performSelector?

Spa*_*Dog 4 iphone cocoa iphone-sdk-3.0 ipad

我有一个专为iPhone OS 2.x设计的应用程序.

在某些时候我有这个代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  //... previous stuff initializing the cell and the identifier

  cell = [[[UITableViewCell alloc] 
     initWithFrame:CGRectZero 
     reuseIdentifier:myIdentifier] autorelease]; // A


  // ... more stuff
}
Run Code Online (Sandbox Code Playgroud)

但由于initWithFrame选择器在3.0中已弃用,我需要使用respondToSelector和performSelector转换此代码......因此...

if ( [cell respondsToSelector:@selector(initWithFrame:)] ) { // iphone 2.0
  // [cell performSelector:@selector(initWithFrame:) ... ???? what?
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如果我必须传递两个参数"initWithFrame:CGRectZero"和"reuseIdentifier:myIdentifier",我如何将A上的调用断开到preformSelector调用?

编辑 - 由于fbrereto的消化,我做到了这一点

 [cell performSelector:@selector(initWithFrame:reuseIdentifier:)
    withObject:CGRectZero 
    withObject:myIdentifier];
Run Code Online (Sandbox Code Playgroud)

遇到的错误是"performSelector:withObject:withObject"的参数2的不兼容类型.

myIdentifier是这样声明的

static NSString *myIdentifier = @"Normal";
Run Code Online (Sandbox Code Playgroud)

我试图将呼叫改为

 [cell performSelector:@selector(initWithFrame:reuseIdentifier:)
    withObject:CGRectZero 
    withObject:[NSString stringWithString:myIdentifier]];
Run Code Online (Sandbox Code Playgroud)

没有成功...

另一点是CGRectZero不是一个对象......

ken*_*ytm 11

使用NSInvocation.

 NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
                        [cell methodSignatureForSelector:
                         @selector(initWithFrame:reuseIdentifier:)]];
 [invoc setTarget:cell];
 [invoc setSelector:@selector(initWithFrame:reuseIdentifier:)];
 CGRect arg2 = CGRectZero;
 [invoc setArgument:&arg2 atIndex:2];
 [invoc setArgument:&myIdentifier atIndex:3];
 [invoc invoke];
Run Code Online (Sandbox Code Playgroud)

或者,objc_msgSend直接调用(跳过所有不必要的复杂高级构造):

cell = objc_msgSend(cell, @selector(initWithFrame:reuseIdentifier:), 
                    CGRectZero, myIdentifier);
Run Code Online (Sandbox Code Playgroud)