0

私はそれらの質問を調べましたが、私が欲しいものを正確に見つけることができませんでした. UITableView と ScrollView 内の別のビュー

テーブル ビューを複数のデータ ソースと共有するために、UIScrollView のスクロールをプログラムで強制的に停止します。

私は次のことをしようとしています: UIScrollView 内で、いくつかのコントローラーを備えた UIView を用意します。

それらの下に、UISegmentedControl のようなものがあります。その下には UITableView があります。UISegmentedControl は、UITableView のコンテンツを切り替えます。

すべてUIScrollViewにあります。

私が達成したいのは、下にスクロールしたときに、UISegmentedControl をビューの上部にとどめたいということです。下にスクロールし続けると、UITableView のコンテンツだけがスクロールされます。

それは可能ですか?ありがとう!

4

2 に答える 2

1

あなたが求めていることを理解したと仮定すると、次の方法でこれを行います。

  1. 2 つのセクションで 1 つの UITableView を使用する
  2. 表の最初のセクションに「コントローラーのカップル」を入れます。どのようにするかはあなた次第です(単一のセルか何か)。最初のセクションのコンテンツを要求するときに、UITableViewDataSource にこれらのコントロールを返すように指示するだけです。
  3. セクションが1の場合の UITableViewDelegate メソッド
    (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section で、セグメント化されたコントロールを含むビューを構築し、それを返します。
  4. データ ソースが2 番目のセクションのコンテンツを求められるたびに、セグメント化されたコントロールの状態を確認し、適切なデータを返します。
  5. セグメント化されたコントロールが変更されるたびに、イベントをキャッチしてテーブル ビューをリロードします。このように、手順 4 で新しいデータが 2 番目のセクションに表示され、表の内容が変更されます。

ここでの秘訣は、スクロール中に UITableView セクション ヘッダーがテーブルの上部に固定されるため、探しているものを正確に実現できることです。テーブルの最初のセクションはスクロールして離れ、セグメント化されたコントローラーは一番上に固定され、セクション 2 はその下にスクロールし続けます。

于 2013-04-28T17:00:05.457 に答える
0

2 つのセクションで UITableView を実装しました。最初のセクションにはヘッダーがなく、ヘッダーの高さは 0 です。

最初のセクションの最初で唯一のセルは、スクロール可能にしたい UIView です (赤い背景)。2 番目のセクションのヘッダーは UISegmentedControl (緑の背景) です。

- (void)viewDidLoad {
    [super viewDidLoad];

    data = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6",@"7", @"8", @"9",@"10",nil];

    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    table.delegate = self;
    table.dataSource = self;

    [self.view addSubview:table];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    //Return zero for the first section's header.
    if (section == 0)
        return 0;
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Return nil for the first section's header view.
    if (section == 0) return nil;

    head1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    head1.backgroundColor = [UIColor greenColor];

    return head1;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) return 1;

    return data.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //First section - put the UIView that will be scrollable
    if (indexPath.section == 0)
    {
        static NSString *CellIdentifier = @"ControlCell1";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        head.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:head];

        return cell;
    }

    static NSString *CellIdentifier = @"ControlCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }   
    cell.textLabel.text = [data objectAtIndex:indexPath.row];
    return cell;
}
于 2013-04-29T05:50:02.790 に答える