ios 使用 xib 文件创建自定义 UITableViewCell 的步骤
Posted
技术标签:
【中文标题】ios 使用 xib 文件创建自定义 UITableViewCell 的步骤【英文标题】:ios steps to create custom UITableViewCell with xib file 【发布时间】:2011-02-06 23:49:20 【问题描述】:我需要使用 xib 文件创建自己的 UITableViewCell 来绘制图形界面... 创建新类并将其用于我的 UITableView 的正确步骤是什么?
提前致谢
【问题讨论】:
【参考方案1】:在 ios5 中你会想要使用新的:
registerNib:forCellReuseIdentifier
:
基本上做同样的事情......
【讨论】:
酷,没注意到那个。谢谢! 这就是我想要记住的!不错。 更具体地说,将其添加到您的 viewDidLoad:[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"CustomCell"];【参考方案2】:就reuseIdentifier
而言,我个人认为这两个建议的教程都有很大的缺陷。如果您忘记在界面生成器中分配它或拼写错误,您将在每次调用 cellForRowAtIndexPath
时加载 nib。
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(在 iOS 4.0 及更高版本中可用)在这里用作单例,因为尽管使用了 reuseIdentifier
,但单元格仍会重新初始化大约 10 次左右。现在cellForRowAtIndexPath
看起来像这样:
- (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;
【讨论】:
在我看来,这是最好的方式。谢谢你。如果这是我的主题,我会将其标记为回复。【参考方案3】:已经制作了一个video tutorial,展示了如何使用 Xcode 4.2 执行此操作。作者也写了blog post。
【讨论】:
【参考方案4】:本教程将带您了解整个现代 iOS 5 解决方案,从创建单元格的 xib 和类文件的过程一直到完成:
http://mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB/
【讨论】:
【参考方案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;
【讨论】:
【参考方案6】:您可以使用继承自 UITableViewCell 的 XIB 创建 CustomCell 类。我们将通过以下方式在 tableview 类 .m 文件中添加类别。我认为这是用于自定义单元格创建的最简单的方法。
@interface UITableViewCell(NIB) @property(nonatomic,readwrite,copy) NSString *reuseIdentifier; @结尾 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 返回 30; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 静态 NSString *identifier=@"cell"; CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 如果(细胞==无) NSLog(@"新单元格"); NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 单元格=[笔尖 objectAtIndex:0]; cell.reuseIdentifier=标识符; 别的 NSLog(@"重用单元格"); cell.lbltitle.text=[NSString stringWithFormat:@"Level %d",indexPath.row]; id num=[_arrslidervalues objectAtIndex:indexPath.row]; cell.slider.value=[num floatValue]; 返回单元格; @结尾【讨论】:
以上是关于ios 使用 xib 文件创建自定义 UITableViewCell 的步骤的主要内容,如果未能解决你的问题,请参考以下文章
iOS:dequeueReusableCellWithIdentifier 返回单元格,其中 IBOutlets 为 nil