2011-07-20 47 views
0

任何人都可以解釋我的NSArray獲得什麼之後這段代碼呢....獲取UITableView部分和行後......這段代碼做了什麼?

- (UIViewController *)sampleForIndexPath:(NSIndexPath *)indexPath { 

    NSArray *samples = [samples_ objectAtIndex:indexPath.section]; 
    Class clazz = [samples objectAtIndex:indexPath.row]; 
    UIViewController *instance = [[clazz alloc] initWithNibName:nil bundle:nil]; 
    return [instance autorelease]; 
    } 

我得到科NSArray的......那如何才能指定行的值到類?

回答

2

這裏array samples包含類別的對象。您可以直接使用類名稱或使用對象/變量來創建類的實例。例如,

/* One Way */ 

// Create an instance of MyViewController deirectly 
UIViewController *vc = [[MyViewController alloc] init]; 

/* Another Way */ 

// The following line returns a class object 
Class cls = NSClassFromString(@"MyViewController"); 
// The below is just for an example. This also returns a class object 
Class cls = [MyViewController class]; 
// Create an instance of MyViewController from the class object 
UIViewController *vc = [[cls alloc] init]; 

代碼使用第二方式從由[樣品objectAtIndex:indexPath.row]返回類對象分配視圖控制器對象

1

得到陣列之後,它檢索一個特定Class使用:

Class clazz = [samples objectAtIndex:indexPath.row];

,然後將其使用類實例化UIViewController對象,並返回該對象:

UIViewController *instance = [[clazz alloc] initWithNibName:nil bundle:nil]; 
return [instance autorelease];