2011-12-01 31 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的。取決於你的目的