0

UIScrollView に UIButtons を配置する必要があります。私が持っているコードは機能しますが、持っている量によってはdataItems、間隔が均一ではありません。

問題のスニペット

CGRectMake(10, ((120 / (count + 1)) * (i + 1) * 3) ,300,50)

具体的には

((120 / (count + 1)) * (i + 1) * 3)

作業コード

int count = [dataItems count];  /*  not a specific value, can grow  */
for (int i = 0; i < count; i++) {
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton setTag:i];
    [aButton setFrame: CGRectMake(10,((120 / (count + 1)) * (i + 1) * 3) ,300,50) ];
    [aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
    [scroller addSubview:aButton];
}

スクリーンショット

右側の例は、間隔に関しては左側の例のように見えるはずです。はのUIButtons上に座っているUIScrollViewので、UIScrollViewcontentSizeさらにある場合は も成長し、dataItemsたとえば 30+ がある場合にボタンが画面外にスクロールできるようにする必要がありますdataItems

         ここに画像の説明を入力

4

3 に答える 3

4

セットのサイズとパディングが必要なようですね...

int count = [dataItems count];
CGFloat staticX = 10; // Static X for all buttons.
CGFloat staticWidth = 300; // Static Width for all Buttons.
CGFloat staticHeight = 50; // Static Height for all buttons.

CGFloat staticPadding = 10; // Padding to add between each button.

for (int i = 0; i < count; i++) {
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton setTag:i];
    //Calculate with the Static values.
    //I added one set of padding for the top. then multiplied padding plus height for the count.
    // 10 + 0 for the first
    // 10 + 60 for the second.
    // 10 + 120 for the third. and so on.
    [aButton setFrame: CGRectMake(10,(staticPadding + (i * (staticHeight + staticPadding)) ,staticWidth,staticHeight) ];
    [aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
    [scroller addSubview:aButton];
}

ただし、この種の動作を行うボタンを取得しようとしている場合。私は残りに同意する傾向があります..ボタンを含むカスタムセルを作成できます。

次に、テーブルがボタンを要求したときに、そのボタン セルをロードできます。

そして、アイテムの合計について、テーブルを [dataItems count] に結び付けます。

唯一の計算は、最初に dataItems カウントを設定するときにセルの高さを設定することです。短くしすぎないように注意してください。残りはテーブルが処理します。

于 2011-05-11T00:56:24.050 に答える
1

私はGurpartapに同意しますが、あなたがこれに固執するなら、私はそれを少し違う方法で行います. その setFrame ステートメントの数学を今理解したとしても、後で理解できるでしょうか。私は確かにそれをすばやく見ていません。次のようなことをしてみませんか:

CGRect frm = CGRectMake(10,10,300,50);
for (int i = 0; i < count; i++) {
  UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  [aButton setFrame: frm];
  frm.origin.y = frm.origin.y + aButton.frame.size.height + 10;
  etc, etc
于 2011-05-11T00:43:22.290 に答える
0

代わりに UITableView を実際に使用する必要があります。スクロール ビュー、セルのレイアウトなどをすべて単独で行います。セルの内容を指定するだけです。

于 2011-05-11T00:33:16.853 に答える