Ios 使用情节提要功能的单个UITableView中的多个UITableViewCell
我有类似的观点: 如您所见,有不同的部分Ios 使用情节提要功能的单个UITableView中的多个UITableViewCell,ios,ipad,uitableview,storyboard,Ios,Ipad,Uitableview,Storyboard,我有类似的观点: 如您所见,有不同的部分 等级(1级、2级) 课程>课程(课程1、课程2) 课程>课程>科目(科目1、科目2) 所有这3个单元的用户界面都略有不同 我在我的故事板中创建了1个UITableView,其中3个不同的单元也添加了所需的设计 现在接下来要做的是,我已经搜索过了,我发现的是我们用于XIB的旧解决方案(每个单元3个不同的XIB) 注意-主要问题是如何在课程数据周围创建一个方框。每个方框仅指特定课程。在框(当然)内,有一个主题列表放置在带边框的单元格中。如何使用Cell
- 等级(1级、2级)
- 课程>课程(课程1、课程2)
- 课程>课程>科目(科目1、科目2)
Mrunal要使不同的单元格在UITableView中正常工作,您应该为每个单元格使用不同的重用标识符在故事板中,您只需使用inspector在单元格上设置此属性即可。在代码中,您应该在
-tableView:cellforrowatinexpath:
中执行此操作
当您滚动单元格时,这将使表格工作得很好,并且不会发生奇怪的事情。这样,给定类型的课程将仅使用一种单元格显示
例如,在代码中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * const Course1CellIdentifier = @"Course1Cell";
static NSString * const Course2CellIdentifier = @"Course2Cell";
static NSString * const Course3CellIdentifier = @"Course3Cell";
NSString *reuseIdentifier = nil;
Course *course = ...; // some logic
if (/*check if is course 1*/) {
reuseIdentifier = Course1CellIdentifier;
}
else if (/*check if is course 2*/) {
reuseIdentifier = Course2CellIdentifier;
}
else {
reuseIdentifier = Course1CellIdentifier;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWitStyle:UITableViewStyleDefault reuseIdentifier:reuseIdentifier];
}
...
return cell;
}
此外,对于单元格,您可能希望为课程(实际上不使用的类)创建一个自定义抽象单元格,并将其子类化,以使不同的课程以不同的方式显示(例如,不同单元格的高度、边框、背景等)。您可以使用ImageView执行以下操作:---- 截图---
以下是为您的每一项设计指定不同的标识符,并返回cellForRowAtIndexPath:方法无效所需的标识符?@Raj:谢谢您的回复。但是如果你能看到背景,边框和单元格的位置。如何根据选定的单元格标识符获取所有这些功能?@Raj:我已经完成了索引和字体。现在只剩下一件事了,那就是更大的正方形。如图所示,球场被两个大正方形背景隔开。如何做到这一点。任何建议都可以。您需要三个自定义tableViewCell吗。我认为“学习路径名称”可以包含在第一节的节头中。由于使用分组tableViewStyle可以获得类似的外观,因此不需要课程背景。但不是这样。为此,您可以考虑使用四种类型的细胞图像,用于顶部、中间、底部和顶部底部。有关更多详细信息,请参阅此。
- (void)viewDidLoad
{
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
classes=[[NSArray alloc] initWithObjects:@"Class1",@"Class2",@"Class3",@"Class4",@"Class5",nil];
courses=[[NSArray alloc] initWithObjects:@"Diploma",@"B.Tech",@"NIIT",@"Web Designing",@"Animation", nil];
subjects=[[NSArray alloc] initWithObjects:@"Math",@"Applied Physics",@"Communication Skills", nil];
[_tableView reloadData];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [classes count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [subjects count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *label;
label=(UILabel*)[cell viewWithTag:22];
UIImageView *backImg;
backImg=(UIImageView*)[cell viewWithTag:21];
CGPoint origin=cell.textLabel.frame.origin;
int leftOffset=20;
if (!backImg) {
backImg=[[UIImageView alloc] initWithFrame:CGRectMake(origin.x+leftOffset, origin.y, 150, 30)];
backImg.tag=21;
backImg.image=[UIImage imageNamed:@"back.png"];
label=[[UILabel alloc] initWithFrame:backImg.frame];
label.tag=22;
label.backgroundColor=[UIColor clearColor];
[cell addSubview:backImg];
[cell addSubview:label];
}
CGSize size=[[courses objectAtIndex:indexPath.section] sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 30) lineBreakMode:label.lineBreakMode];
if (indexPath.row==0) {
[backImg setFrame:CGRectMake(origin.x+leftOffset, 5, size.width, size.height)];
label.text=[courses objectAtIndex:indexPath.section];
}else{
[backImg setFrame:CGRectZero];
label.text=[subjects objectAtIndex:indexPath.row-1];
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
label.font=[UIFont boldSystemFontOfSize:17.0];
label.text=[classes objectAtIndex:section];
label.backgroundColor=[UIColor clearColor];
CGSize size=[[classes objectAtIndex:section] sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 30) lineBreakMode:label.lineBreakMode];
NSLog(@"size at section %i is %@",section,NSStringFromCGSize(size));
UIImageView *imgView;
imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 5, size.width, size.height)];
[imgView setImage:[UIImage imageNamed:@"back.png"]];
UILabel *backView=[[UILabel alloc]initWithFrame:label.frame];
[backView setBackgroundColor:[UIColor clearColor]];
[backView addSubview:imgView];
[backView addSubview:label];
return backView;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [classes objectAtIndex:section];
}