【发布时间】:2010-10-27 21:27:44
【问题描述】:
我有以下代码:
+ (UITableViewCell *) createTableViewCell: (NSString *) cell_id withTableView: tableView {
SomeViewClass *cell = (SomeViewClass *)[tableView dequeueReusableCellWithIdentifier: cell_id];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed: @"AViewCell"
owner: nil
options: nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass: [UITableViewCell class]]) {
cell = (SomeViewClass *) currentObject;
break;
}
}
}
return cell;
}
我想抽象该代码,以便它可以与任何视图类一起使用,而不仅仅是“SomeViewClass”。我想把它变成如下的东西。请注意下面动态类型(类 *)的转换。
+ (UITableViewCell *) createTableViewCell: (NSString *) cell_id withTableView: tableView withClass: (Class) a_class {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cell_id];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed: @"AViewCell"
owner: nil
options: nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass: [UITableViewCell class]]) {
cell = (a_class *) currentObject;
break;
}
}
}
return cell;
}
这会引发编译时错误。我必须求助于宏吗?有没有更好的方法来做到这一点?
【问题讨论】:
标签: ios objective-c