2014-07-22 212 views
1

我在应用程序中使用静态单元格的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这里,如果它是可能的。

+0

你为什么不在故事板上做? –

+0

这个的实际目的是实现在线拾取器单元。从故事板延迟加载视图(3-4秒),加载更多的捡取器(在我的情况下是4个拾取器)。我不想这样做。 – Sibin

+0

从Storyboard加载2个细胞是不可能的,需要3-4秒。如果你真的想要避免Storyboard,你可以将它们放在xib文件中。 – Szu

回答

1

您可以使用indexPath.row属性来查看您的目标行是否小于第6行,如果是这种情况,则将不是您的自定义单元的单元出列。

首先创建自定义细胞,并给它的标识符(“myCustomCellReuseId)然后在您的视图控制器使用:

[tableview registerNib:[UINib nibWithName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"myCustomCellReuseId"]; 

然后,在原型细胞在故事板,得到默认细胞中比不同的标识符你给你的自定义单元格的一个

然后,在你-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用:

if(indexPath.row > 5) { 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCellReuseId"]; 
} else { 
    // 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

} 
+0

我不知道该怎么做。在我的情况下,前6行必须是故事板的默认单元格,其余2个单元格是代码。可能吗?如果可以,请提供代码。谢谢。 – Sibin

+0

请参阅我编辑的答案。 – KerrM

+0

我知道如何使用动态细胞类型。我正在尝试使用所有的静态单元格,这使我无法编写大量代码。从所有的回应中我都认为在这种情况下不可能使用静态单元格。我想我的问题不清楚:P。感谢您的帮助。 – Sibin

1

由于@Sviatoslav Yakymiv提到的设计单元格的最简单方法是将Storyboard与程序化自定义混合在一起:您将在Storyboard中完成的布局,但是您将在视图控制器.m文件中更改的内容。这意味着您在-initWithStyle: reuseIdentifier:中的所有代码都可以在IB中进行设计。然后,您可以在IB中创建2个不同的动态原型。换句话说,您可以将您的自定义单元格与默认的UITableViewCell混合。例如在Interface Builder你有动态原型细胞:

  1. 默认的UITableViewCell与[email protected]"Cell"

  2. [email protected]"MyCustomCell"自定义单元格(您可以在右上角的Indentity Inspector中更改它)。

如果你会做正确,您将不LOGEN需要使用这3行:

if (cell == nil) { 
     cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"MyCustomCell"]; 
    } 

那么您应该将功能更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row >= rowWhereCustomCellShouldShow && indexPath.section > yourSection) { 
     MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"]; 
     [cell customizeForData:data[indexPath.row]; 
     return cell; 
    } else { // The apple default cell 
     UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
     // Here you can customize your cell. 
     return cell2; 
    } 
} 
+0

我知道如何使用动态细胞类型。我试图在这种情况下使用所有静态单元来避免额外的代码。从所有的回应中我都认为在这种情况下不可能使用静态单元格。谢谢您的帮助。 – Sibin