我在应用程序中使用静态单元格的UITableViewController
。有没有什么办法可以在表视图中使用默认单元格以及代码中的子类单元格?我的tableview有8行,其中6行希望在tableview中使用默认单元格。对于剩余的两个单元格,我想通过代码创建它。使用静态单元格对Tableview中的单元格进行子类化
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"MyCustomCell"];
}
return cell;
}
而在MyCustomCell.m包含,
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
self.myLabel.backgroundColor = [UIColor clearColor];
self.myLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
self.myLabel.textAlignment = NSTextAlignmentCenter;
self.myLabel.text = @"Hi there";
[self.contentView addSubview:self.myLabel];
}
return self;
}
的-tableView:CellForRowAtIndexPath:
方法有助于创建custom cells
的代码,但我不知道知道如何访问默认cells
这里,如果它是可能的。
你为什么不在故事板上做? –
这个的实际目的是实现在线拾取器单元。从故事板延迟加载视图(3-4秒),加载更多的捡取器(在我的情况下是4个拾取器)。我不想这样做。 – Sibin
从Storyboard加载2个细胞是不可能的,需要3-4秒。如果你真的想要避免Storyboard,你可以将它们放在xib文件中。 – Szu