Ios 使用静态单元格对Tableview中的单元格进行子分类
我有一个应用程序中带有静态单元格的Ios 使用静态单元格对Tableview中的单元格进行子分类,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个应用程序中带有静态单元格的UITableViewController。是否有任何方法可以使用表视图中的默认单元格以及按代码划分的子类单元格?我的tableview有8行,其中6行希望使用tableview中的默认单元格。对于剩下的两个单元格,我想通过代码创建它 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCus
UITableViewController
。是否有任何方法可以使用表视图中的默认单元格以及按代码划分的子类单元格?我的tableview有8行,其中6行希望使用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:
方法有助于按代码创建自定义单元格
,但如果可能,我不知道如何在此处访问默认的单元格
。您可以使用indexPath.row
属性查看目标行是否小于第6行,如果是这样的话将非自定义单元格的单元格出列
首先创建自定义单元格并给它一个标识符(“myCustomCellReuseId”),然后在视图控制器中使用:
[tableview registerNib:[UINib nibWithName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"myCustomCellReuseId"];
然后,在故事板中的原型单元中,为默认单元指定一个与自定义单元不同的标识符
然后,在您的-(UITableViewCell*)表视图中:(UITableView*)表视图cellForRowAtIndexPath:(nsindepath*)indepath
使用:
if(indexPath.row > 5) {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCellReuseId"];
} else {
//
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}
正如@Sviatoslav Yakymiv所提到的,设计单元的最简单方法是将故事板与编程定制相结合:在故事板中完成布局,但在视图控制器
.m
文件中更改内容。这意味着-initWithStyle:reuseIdentifier:
中的所有代码都可以在IB.T中设计然后,您可以在IB中创建两个不同的动态原型。换句话说,您可以将自定义单元格与默认的UITableViewCell
混合使用。例如,在Interface Builder中,您有动态原型单元格:
reuseIdentifier=@“Cell”
reuseIdentifier=@“MyCustomCell”
自定义单元格(您可以在右上角的标识检查器中对其进行更改) 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;
}
}
为什么不在情节提要中执行此操作?实际目的是实现内嵌选择器单元格。从情节提要加载更多选择器(在我的示例中为4个选择器)会延迟加载视图(3-4秒)。我不想这样做。从故事板加载2个单元格不可能需要3-4秒。如果你真的想避免使用故事板,可以将它们放在xib文件中。我在iPhone 4上进行了测试,速度有点慢。我不知道该怎么做。在我的情况下,前6行必须是故事板中的默认单元格,其余2行必须是代码中的默认单元格。是否可行ible?如果可以,请您提供代码。谢谢。我知道如何使用动态单元格类型。我尝试使用所有静态单元格,这样可以避免我编写大量代码。从所有的回答中,我认为在这种情况下不可能使用静态单元格。我想我的问题不清楚:P。感谢您的帮助。我知道如何使用静态单元格使用动态单元格类型执行此操作。我尝试在本例中使用所有静态单元格以避免额外代码。从所有响应中,我认为在本例中不可能使用静态单元格。谢谢您的帮助。