2011-02-06 10 views

答えて

6

のXcode 4.2でこれを行う方法を示すvideo tutorialが行われました。著者はblog postも書いています。

10

個人的には、両方のチュートリアルのチュートリアルには、reuseIdentifierという大きな欠陥があります。インターフェイスビルダーでそれを割り当てることを忘れたり、スペルミスを忘れた場合は、cellForRowAtIndexPathが呼び出されるたびにペン先をロードします。

Jeff LaMarcheはこれについて、このblog postで修正する方法について書いています。 reuseIdentifierのほかに、彼はLoading Custom Table-View Cells From Nib Filesのリンゴのドキュメンテーションと同じアプローチを使用しています。

編集:あなたはiOSの5.0以降をターゲットにしている場合は、Duane Fields' answer

@interface CustomCellWithXib : UITableViewCell 

+ (NSString *)reuseIdentifier; 
- (id)initWithOwner:(id)owner; 

@end 

@implementation CustomCellWithXib 

+ (UINib*)nib 
{ 
    // singleton implementation to get a UINib object 
    static dispatch_once_t pred = 0; 
    __strong static UINib* _sharedNibObject = nil; 
    dispatch_once(&pred, ^{ 
     _sharedNibObject = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; 
    }); 
    return _sharedNibObject; 
} 

- (NSString *)reuseIdentifier 
{ 
    return [[self class] reuseIdentifier]; 
} 

+ (NSString *)reuseIdentifier 
{ 
    // return any identifier you like, in this case the class name 
    return NSStringFromClass([self class]); 
} 

- (id)initWithOwner:(id)owner 
{ 
    return [[[[self class] nib] instantiateWithOwner:owner options:nil] objectAtIndex:0]; 
} 

@end 

UINib(に固執したいと思う私は、次のコードを思いついたすべてのこれらの記事を読んだ後

reuseIdentifierが使用されていますが、セルはまだ約10回程度再初期化されるため、ここではシングルトンとして使用されています。 iOS5をで

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCellWithXib *cell = [tableView dequeueReusableCellWithIdentifier:[CustomCellWithXib reuseIdentifier]]; 
    if (cell == nil) { 
     cell = [[CustomCellWithXib alloc] initWithOwner:self]; 
    } 

    // do additional cell configuration 

    return cell; 
} 
+0

私の意見では、これが最良の方法です。ありがとうございます。それが私の話題だったら、私はそれを返答としてマークします。 –

15

新しいを使用したいと思う:

registerNib:forCellReuseIdentifier:基本的には同じことを行い

...

+0

クール、それに気付かなかった。ありがとう! – christoph

+0

それは私が覚えようとしていたことです!ニース。 – Ash

+1

具体的には、これをviewDidLoadに追加します:[self.tableView registerNib:[UINib nibWithNibName:@ "CustomCell" bundle:nil] forCellReuseIdentifier:@ "CustomCell"]; –

0

あなたが作成することができます今cellForRowAtIndexPathは次のようになりますUITableViewCellから継承されたXIBを持つCustomCellクラス。次のように、tableviewクラス.mファイルにカテゴリを追加します。私は、これがカスタムセル作成に適用される最も簡単な方法だと思います。

 

    @interface UITableViewCell(NIB) 
    @property(nonatomic,readwrite,copy) NSString *reuseIdentifier; 
    @end 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return 30; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *[email protected]"cell"; 
     CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if(cell==nil) 
     { 
      NSLog(@"New Cell"); 
      NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
      cell=[nib objectAtIndex:0]; 
      cell.reuseIdentifier=identifier; 

     }else{ 
      NSLog(@"Reuse Cell"); 
     } 
     cell.lbltitle.text=[NSString stringWithFormat:@"Level %d",indexPath.row]; 
     id num=[_arrslidervalues objectAtIndex:indexPath.row]; 
     cell.slider.value=[num floatValue]; 
     return cell; 
    } 
    @end 

1

` .xibファイルの助けを借りてテーブルビューのカスタムセル。最初にビューコントローラーでテーブルビューを設定し、そのクラスで新しいxibファイルを作成し、それをテーブルビューで使用します。

- (IBAction)moveToSubCategory:(id)sender; 
@property (strong, nonatomic) IBOutlet UILabel *foodCategoryLabel; 
@property (strong, nonatomic) IBOutlet UIImageView *cellBg; 



-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [foodCatArray count]; 
} 



-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *simpleTableIdentifier = @"ExampleCell"; 
     ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    [cell setTag:indexPath.row]; 
    cell.cellBg.image=[UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]]; 
    cell.foodCategoryLabel.text=[foodCatArray objectAtIndex:indexPath.row]; 
    return cell; 

} 
関連する問題