簡體   English   中英

UITableview標頭和頂部單元格之間的間距

[英]Spacing between UITableview header and top cell

我的UITableView出現一些奇怪的行為。

我的UITableView標頭和第一個單元格之間有間距。 知道為什么會這樣嗎?

#define kDefaultTableViewSectionHeight 50

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.posts.count;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDefaultTableViewCellHeight;
}

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

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

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDefaultTableViewSectionHeight)];
    UIButton *hot = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 0, 100, kDefaultTableViewSectionHeight)];
    UIButton *new = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100, 0, 100, kDefaultTableViewSectionHeight)];
    UIView *underline = [[UIView alloc] initWithFrame:CGRectMake(new.frame.origin.x, view.frame.size.height-10, 70, 1)];
    UIView *border = [[UIView alloc] initWithFrame:CGRectMake(0, view.frame.size.height-1, view.frame.size.width, 1)];

    [border setBackgroundColor:[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1]];

    self.underline = underline;

    if (self.new)
        [underline setCenter:CGPointMake(new.center.x, underline.center.y)];
    else
        [underline setCenter:CGPointMake(hot.center.x, underline.center.y)];

    [underline setBackgroundColor:[UIColor whiteColor]];
    [view setBackgroundColor:[UIColor blackColor]];

    [hot setTitle:@"Hot" forState:UIControlStateNormal];
    [hot setTag:2];
    [hot setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [hot addTarget:self action:@selector(selectQueryType:) forControlEvents:UIControlEventTouchUpInside];
    [hot.titleLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:20.0]];

    [new setTitle:@"New" forState:UIControlStateNormal];
    [new setTag:1];
    [new setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [new addTarget:self action:@selector(selectQueryType:) forControlEvents:UIControlEventTouchUpInside];
    [new.titleLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:20.0]];

    [view addSubview:new];
    [view addSubview:hot];
    [view addSubview:underline];
    [view addSubview:border];
    return view;
}

我已經嘗試使用以下代碼解決此問題,但這只是減少了UITableView頂部與UINavigationBar之間的空間:

    /*
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
        self.postTableview.contentInset = UIEdgeInsetsMake(-8, 0, 0, 0);
    } */

但這只是減小了UITableView頂部和UINavigationbar之間的間距。

在此處輸入圖片說明

我認為您正在將tableHeaderView屬性與節標題視圖混淆。 您正在調用的委托方法tableview:viewForHeaderInSection:設置節的標題,而不是UITableView本身的標題。 在您的-numberOfSectionsInTableView:您僅返回1部分,因此只需將自定義標頭添加為tableview的標頭,而不是將其作為tableview中第一部分的標頭。 這不能解決您的特定問題,但是考慮到表視圖中只有一個部分,這似乎是您應用程序的更好解決方案。

-viewDidLoad您可以簡單地將tableHeaderView屬性設置為自定義視圖,如下所示:

- (void)viewDidLoad{

  //...

  _tableview.tableHeaderView = [self myTableHeader];

}

- (UIView *)myTableHeader{

  //Return custom header view 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM