2010-11-16 44 views
0

我想要在的OBJ-C使用动态类型,我有下面的代码和错误:NSClassFromString - 不工作

alt text

对我来说似乎不错,但不要”不想工作,有什么想法?

回答

1

既然你实际上并没有创建一个新的实例(只检索一个),没有必要为此。这是足够的:

// If we don't know the exact class of the cell, type it with the common superclass of all cells 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rssItemCell"]; 
0

你不能使用变量cellClass作为类型,它是一个对象。

在这种情况下,你必须使用一个id对象或UITableViewCell中。事情是这样的:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rssItemCell"]; 

id cell = [tableView dequeueReusableCellWithIdentifier:@"rssItemCell"]; 
3

因为你在运行时创建cellClass,编译器不知道它,所以它不能编译代码。

这会工作:

Class cellClass = NSClassFromString(@"CustomCell"); 
UITableViewCell *cell; // You could just id here as well if you wanted but you now that CustomCell is definitely a type of UITableViewCell 
cell = [tableView dequeueReusableCellWithIdentifier:@"rssItemCell"]; 

可是,为什么你不这样做呢?

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"rssItemCell"]; 
+0

因为它不会总是CustomCell,它必须是不固定的,谢谢你的帮助,我会测试你的解决方案 – Mapedd 2010-11-16 14:01:17