Objective c 代码实例化表视图的单元格问题
我正在构建一个具有外部委托控制器的表视图,它是由代码而不是脚本创建的。我对单元格有一个问题,它们没有显示,尽管执行正在正确地到达委托方法: 构建方法:Objective c 代码实例化表视图的单元格问题,objective-c,ios5,Objective C,Ios5,我正在构建一个具有外部委托控制器的表视图,它是由代码而不是脚本创建的。我对单元格有一个问题,它们没有显示,尽管执行正在正确地到达委托方法: 构建方法: -(void)buildCategorias{ CGRect twitterFrame = CGRectMake(105, 83, 600, 568); categoriasView = [[UIView alloc] initWithFrame:twitterFrame]; CGRect scrollViewFra
-(void)buildCategorias{
CGRect twitterFrame = CGRectMake(105, 83, 600, 568);
categoriasView = [[UIView alloc] initWithFrame:twitterFrame];
CGRect scrollViewFrame = CGRectMake(105, 83, 400, 568);
categoriasTableView = [[UITableView alloc] initWithFrame:scrollViewFrame];
categoriasController = [[categoriasViewController alloc] init];
categoriasController.categorias = [[NSArray alloc] initWithObjects:@"Gafas", @"Relojes", @"Pantalones", @"Deportivas", @"Cazadoras", nil];
[categoriasTableView setDelegate:categoriasController];
[categoriasTableView setDataSource:categoriasController];
[self.categoriasView addSubview:categoriasTableView];
[categoriasTableView reloadData];
[self.view addSubview:categoriasView];
}
自定义单元格:categoriasCell.h
@interface categoriasCell : UITableViewCell{
UILabel *title;
}
@property (nonatomic, strong) IBOutlet UILabel *title;
@end
阿塞尔
@implementation categoriasCell
@synthesize title;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
表视图委托:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
categoriasCell *cell = [self.tableView
dequeueReusableCellWithIdentifier:@"categorias"];
if (cell == nil) {
cell = [[categoriasCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"categorias"];
}
cell.title.text = [categorias objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
@end
表视图为空,没有内容
非常感谢您的帮助是否完全没有单元格,或者单元格数量是否正确,但它们是空的 如果根本没有单元格,请检查您是否实现了另一个必需的委托方法(
tableView:numberofrowsinssection:
),并且它确实返回了您期望的单元格数
否则,您可能会丢失从其NIB加载单元格的代码(我假设,鉴于您的categoriasCell
中有一个IBOutlet,并且没有代码实际将标题标签添加到视图中,您希望单元格来自NIB)。有关如何加载单元格的NIB并在委托方法中使用它的信息,请参见。如果不从NIB加载,文本字段将不存在,因此单元格将为空
以及两个非致命的编码/样式问题:
- 初始化单元格时,将
作为样式传递。这里确实应该使用UITableViewCellSelectionStyleNone
,因为参数是单元格样式,而不是选择样式UITableViewCellStyleDefault
- 您的类名应该大写(
而不是CategoriasCell
)CategoriasCell