iPhone-如何在复杂参数下使用PerformSelect?
我有一个为iPhone OS 2.x设计的应用程序 在某种程度上,我有这个代码iPhone-如何在复杂参数下使用PerformSelect?,iphone,cocoa,iphone-sdk-3.0,ipad,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]
- (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
}
但由于initWithFrame选择器在3.0中被弃用,我需要使用respondToSelector和performSelector转换此代码。。。就像这样
if ( [cell respondsToSelector:@selector(initWithFrame:)] ) { // iphone 2.0
// [cell performSelector:@selector(initWithFrame:) ... ???? what?
}
我的问题是:如果我必须传递两个参数“initWithFrame:CGRectZero”和“reuseIdentifier:myIdentifier”?,如何将对的调用分解为选择器调用
编辑-根据fbrereto的建议,我做了这个
[cell performSelector:@selector(initWithFrame:reuseIdentifier:)
withObject:CGRectZero
withObject:myIdentifier];
我遇到的错误是'performSelector:withObject:withObject'的参数2的类型不兼容
myIdentifier是这样声明的
static NSString *myIdentifier = @"Normal";
我试着把电话改成
[cell performSelector:@selector(initWithFrame:reuseIdentifier:)
withObject:CGRectZero
withObject:[NSString stringWithString:myIdentifier]];
没有成功
另一点是CGRectZero不是对象…您要使用的选择器实际上是
@selector(initWithFrame:reuseIdentifier:)
。要传递两个参数,请使用。可能需要一些尝试和错误才能获得正确的参数,但它应该可以工作。如果不能,我建议探索用于处理更复杂消息调度的类。使用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];
或者,直接调用
objc\u msgSend
(跳过所有不必要的复杂高级构造):
我不确定你是否可以通过performSelector传递CGRect:根本不是对象。谢谢你的回答。我已经更新了问题,以显示我所做的事情和我所经历的错误。Chpwn在这里也有一个很好的观点…CGRect不是对象…这正在融化我的大脑…@Chpwn:观点很好;似乎Invocation将是最好的(只有?)在这里选择。谢谢。我使用了你的代码,但是我在代码的第一行有一个EXC\u BAD\u访问权限。我想原因是,在最初的调用中,一个ALLOC正在进行,出于某种原因,我不确定这个方法是否正在分配单元格…我尝试了你的第二个方法,我有相同的错误。我发现了解决方案上。你的代码几乎完美。唯一缺少的是先分配单元格。谢谢!
cell = objc_msgSend(cell, @selector(initWithFrame:reuseIdentifier:),
CGRectZero, myIdentifier);