2011-12-01 69 views
2

你好,亲爱的程序员的动态数量,定制的TableView细胞创建与对象

我有下面的方法创建一个自定义tableviewcell要求:


@interface CustomTableCell : UITableViewCell { 

} 

@implementation CustomTableCell 

-(void)setObjectWithType:(NSString *)objectType atPlace:(CGRect) placeOfObject 
{ 
    class className=NSClassFromString(objectType); 

    className *objectName = [[NSClassFromString(objectType) alloc] init];// Giving error  
} 

请解决这个问题,以创建任何类型的通过传递如下参数的对象:


CustomTableCell *cell=[[CustomTableCell alloc] init]; 
[cell setObjectWithType:@"UILabel" atPlace:CGRectMake(0, 0, 100, 30)]; 

谢谢,提前。

+1

这不是太复杂吗?为什么不在调用之前创建对象并传递创建的对象? – Sulthan

回答

1

这听起来像它可能会变成一场噩梦,但要解决你的直接问题,你必须在创建任意对象时使用id或最低公共超类(例如UIView):

id objectName = [[NSClassFromString.... 

编译器无法动态转换您尝试的方式。它必须在运行时完成。

0

使用普通型一样id

id objectName = [[NSClassFromString(objectType) alloc] init]; 

您还可以使用的UIView,NSObject的。取决于你的目的