gpt4 book ai didi

ios - 使用 Storyboard 功能在单个 UITableView 中使用多个 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 10:58:45 25 4
gpt4 key购买 nike

我有类似的观点:

enter image description here

如你所见,有多个部分

  • 类(class)(Class-1、Class-2)
  • 类(class) > 类(class)(类(class) 1、类(class) 2)
  • 类(class) > 类(class) > 科目(科目 1、科目 2)

所有这 3 个单元格的 UI 都略有不同。

我在 Storyboard中创建了 1 个 UITableView,其中还添加了 3 个不同的单元格并进行了必要的设计。

现在下一步该做什么,我已经搜索过了,我发现的是我们过去用于 xib 的旧解决方案(每个单元格有 3 个不同的 xib)

注意- 主要问题是如何围绕类(class)数据创建一个框。每个框仅指特定类(class)。在方框(类(class))内,有一个主题列表位于带边框的单元格中。如何使用 CellIdentifier 和 Storyboard 实现这样的场景?

让我知道我可以申请 Storyboard的通用解决方案。

问候,

鲁纳尔

最佳答案

您可以使用 ImageView 来执行此操作:----

- (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];
}

截图---

enter image description here

这里是 Img

关于ios - 使用 Storyboard 功能在单个 UITableView 中使用多个 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com