グラフィックインターフェイスを描画するために、xibファイルを使用して独自のUITableViewCellを作成する必要があります... 新しいクラスを作成して自分のUITableViewに使用する正しい手順は何ですか?事前ios xibファイルでカスタムUITableViewCellを作成する手順
でおかげ
グラフィックインターフェイスを描画するために、xibファイルを使用して独自のUITableViewCellを作成する必要があります... 新しいクラスを作成して自分のUITableViewに使用する正しい手順は何ですか?事前ios xibファイルでカスタムUITableViewCellを作成する手順
でおかげ
のXcode 4.2でこれを行う方法を示すvideo tutorialが行われました。著者はblog postも書いています。
個人的には、両方のチュートリアルのチュートリアルには、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;
}
あなたが作成することができます今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
このチュートリアルでは、すべての方法仕上がりにセルのXIBファイルとクラスファイルを作成するプロセスからの全体現代のiOS 5ソリューション、を介して表示されます:あなたが作成することができます
` .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;
}
私の意見では、これが最良の方法です。ありがとうございます。それが私の話題だったら、私はそれを返答としてマークします。 –