2012-02-14 6 views
1

UITableViewCellサブクラスを作成しました。現在、それを使用していますHomeViewControllerクラスでは、私はこれを行う:IBでUITableViewCellサブクラスを再利用する方法

@interface: (for HomeViewController) 
@property (nonatomic, assign) IBOutlet UITableViewCell *customCell; 

@implementation: 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CustomTableViewCellIdentifier = @"CustomTableViewCellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomTableViewCellIdentifier]; 
    if (cell == nil) { 
     UINib *cellNib = [UINib nibWithNibName:@"CustomTableViewCell" bundle:nil]; 
     [cellNib instantiateWithOwner:self options:nil]; 
     cell = self.customCell; 
     self.customCell = nil; 
    } 
    NSUInteger row = [indexPath row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

をCustomTableViewCell.xibでは、私のファイルの所有者はHomeViewControllerであり、私はCustomTableViewCellへのファイルの所有者からコンセントを接続します。そのすべてがうまく動作します。

今、このセルを使用するために、DetailViewControllerという別のUIViewControllerのサブクラスを追加したいと思います。 My Fileの所有者オブジェクトは既に使用されています。私はこのセルを再利用するために他のオブジェクトを作成することに精通していません。誰かがこのシナリオで何をする必要があるのか​​説明できますか?ありがとう。

答えて

4

まず、毎回UINibオブジェクトを作成しないでください。一度作成して再利用してください。はるかに高速に実行されます。

第二には、それはあなたがアップ配線しているファイルの所有者の財産だけのように見えるcustomCellです。これですべてのことが必要な場合は、接続を一本化しないほうが簡単です。代わりに、セルがペン先の最初または唯一のトップレベルのオブジェクトであることを確認します(ペン先のアウトラインの[オブジェクト]セクションの最初のトップレベルオブジェクトにします)。次に、このようにアクセスできます:

+ (UINib *)myCellNib { 
    static UINib *nib; 
    static dispatch_once_t once; 
    dispatch_once(&once, ^{ 
     nib = [UINib nibWithNibName:@"CustomTableViewCell" bundle:nil]; 
    }); 
    return nib; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CustomTableViewCellIdentifier = @"CustomTableViewCellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomTableViewCellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelNibObjects = [self.class.myCellNib instantiateWithOwner:nil options:nil]; 
     cell = [topLevelNibObjects objectAtIndex:0]; 
    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 
関連する問題