2

2つのセクションを持つtableViewがありますセクションヘッダーにボタンを追加したい(アクションボタン)サンプル画像でも同じことができますか?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

if(section ==0)
{
    return 0;
}
else{
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)];
    button.tag = section;
    button.hidden = NO;
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown];
    [myView addSubview:button];
    return myView;    }
}

ここに画像の説明を入力してください

4

4 に答える 4

7

このように試してみてくださいそれはあなたを助けるかもしれません、

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //Headerview
    UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)] autorelease];
     UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)];
    button.tag = section;
    button.hidden = NO;
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown];
    [myView addSubview:button];
      return myView;
}

ここでヘッダーセクションの高さを変更できます

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

同じボタン画像を取得するために、カスタム画像を取得できます。デフォルトの画像を使用すると、そのように表示することはできません。

于 2013-03-15T08:52:50.833 に答える
1

はい、それは可能です。そのためには、UITableView Delegateメソッド
を使用してカスタムを作成Viewし、そこから戻る必要があります。UIButton

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 ///your implementation
  return customView;
}

ビューの高さを20ではなく45にします。小さすぎます。

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)];
于 2013-03-15T08:55:39.297 に答える
1

コードの問題はbuttonフレームが(275.0, 5.0, 30.0, 30.0)ですが、myViewフレームは(0.0, 0.0, 300.0, 20.0)です。の高さbuttonは30ですが、myView20のみです。のY位置buttonは5なので、myView高さは40である必要があります。

myViewフレームを(0.0、0.0、300.0、40.0)に変更します。

また、セクションの高さもheightForHeaderInSection:デリゲートメソッドを使用して40に設定されています。

この手順に従うだけです。

  1. myViewフレームをとして設定し(0.0, 0.0, 300.0, 40.0)ます。
  2. buttonフレームをとして設定し(275.0, 5.0, 300.0, 20.0)ます。
  3. heightForHeaderInSectionデリゲートメソッドを使用して、セクションの高さを40に設定します。

これはあなたの問題を解決します。

于 2013-03-15T09:17:56.353 に答える
0

ヘッダービューの高さを設定します

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 81;

}
于 2014-05-10T06:50:07.967 に答える