3

UITableViewのヘッダーとセルの間隔を広げたい。

私はたくさん検索しましたが、この解決策を見つけました、

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

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   //Adding UIView and it's Background Image...
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
    UIImageView *BgImg=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,20)];
    BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
    [tempView addSubview:BgImg];

    //Adding UIImageView...
    UIImageView *flag=[[UIImageView alloc]initWithFrame:CGRectMake(10,3,20,12)];
    flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
    [tempView addSubview:flag];

    //Adding UILabel...
    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(40,1,300,15)];
    tempLabel.backgroundColor=[UIColor colorWithRed:15.0/255.0 green:15.0/255.0 blue:30.0/255.0 alpha:1.0];
    tempLabel.textColor=[UIColor whiteColor];
    tempLabel.font=[UIFont systemFontOfSize:11];
    tempLabel.text=@"United Kingdom";
    [tempView addSubview: tempLabel];

    return tempView;
}

このコードを試してみましたが、シミュレーターでは正常に動作していますが、デバイスで実行するとスペースが表示されません。

これがシミュレーターのスナップショットです: (ヘッダーとセルの間にギャップが見えます..) : よろしくお願いします。

4

4 に答える 4

3

これを試して:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50.0;
}
于 2013-07-30T10:27:49.697 に答える
0

スウィフト 4.2

1-カスタムヘッダークラ​​スを作成して、背景を明確にします。

class CustomHeader: UITableViewHeaderFooterView{
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundView = UIView(frame: self.bounds)
        backgroundView!.backgroundColor = .clear
    }
} 

2- .xib ファイルを作成し、作成したばかりのクラスを割り当てます。

3- .xib ファイルのカスタム ヘッダー内に背景ビューを作成します。

4-この背景ビューに下部パディングを与え、ヘッダー ビュー サブビューのコンテナーとして動作させます。

5-カスタムヘッダーをテーブルビューに登録することを忘れないでください。

tableView.register(UINib(nibName: "NibName", bundle: nil), forHeaderFooterViewReuseIdentifier: "Identifier")

6-いつでもヘッダーを使用してください。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader
    // Customize your header
    return header
}

7-ヘッダーをカスタマイズする必要がなく、カスタム ヘッダーを 1 つしか使用しない場合は、テーブル ビューを設定する場所でこのショートカットを使用できます。

yourTableView.tableHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader 
于 2018-11-29T11:43:36.393 に答える