1

私は次のようなビューを持っています:

ここに画像の説明を入力

ご覧のとおり、さまざまなセクションがあります

  • クラス(クラス1、クラス2)
  • クラス > コース (Course-1、Course-2)
  • クラス > コース > 科目 (科目-1、科目-2)

これら 3 つのセルすべての UI は少し異なります。

ストーリーボードに 1 つの UITableView を作成しました。3 つの異なるセルも必要な設計で追加されています。

次に何をすべきか、私はすでにこれを検索しています.私が見つけたのは、xibsに使用していた古いソリューションです(セルごとに3つの差分xibs)

- 主な問題は、コース データの周りにボックスを作成する方法のようなものです。各ボックスは、特定のコースのみを指します。ボックス(コース)内には、枠で囲まれたセルに科目リストが配置されています。CellIdentifier とストーリーボードを使用してそのようなシナリオを実現するにはどうすればよいですか?

ストーリーボードに適用できる一般的なソリューションを教えてください。

よろしく、

ムルナル

4

2 に答える 2

2

これを行うには 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];
}

スクリーンショット---

ここに画像の説明を入力

ここは画像

于 2013-06-15T11:08:09.793 に答える
0

UITableView でさまざまなセルを適切に機能させるには、セルごとに異なる再利用識別子を使用する必要があります。Storyboards では、インスペクタを使用してセルにこのプロパティを簡単に設定できます。コードでは、 で行う必要があります-tableView:cellForRowAtIndexPath:

これにより、セルをスクロールするときにテーブルがうまく機能し、奇妙なことが発生しなくなります。このようにして、特定のタイプのコースは、1 種類のセルのみを使用して表示されます。

たとえば、コードでは次のようになります。

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

さらに、セルについては、コース (実際には使用しないクラス) 用のカスタム抽象セルを作成し、サブクラス化して、異なるコースを異なる方法で表示することができます (たとえば、異なるセルの高さ、境界線、背景、なんでもいい)。

于 2013-06-15T09:08:40.640 に答える