簡體   English   中英

UITableview節標題不顯示文本

[英]UITableview Section Header not showing text

我已將此代碼添加到我的UITableViewController中,以返回灰色標題部分。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
   [headerView setBackgroundColor:[UIColor grayColor]];
    return headerView;
}

但是現在執行此操作時,我看不到標題字符...我是否需要將此添加為子視圖? 還是有另一種做事方式?

我有這些方法將標題和標題文本添加到UITablView,但是當我使用上述方法時,我再也看不到它們了。

// Section headers
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{    
    return [sectionLetterArray objectAtIndex:section];
}

// Section header titles
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return sectionLetterArray;
}

如果創建自己的tableView:viewForHeaderInSection ,則必須創建UILabel或其他內容,然后自己填寫文本,例如:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    [headerView setBackgroundColor:[UIColor grayColor]];

    // Add the label
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSectionTitleLeftMargin, 
                                                                     kSectionTitleTopMargin, 
                                                                     tableView.bounds.size.width - kSectionTitleLeftMargin - kSectionTitleRightMargin, 
                                                                     30.0 - kSectionTitleTopMargin - kSectionTitleBottomMargin)];

    // do whatever headerLabel configuration you want here

    headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    [headerView addSubview:headerLabel];

    // Return the headerView
    return headerView;
}

顯然,無論如何都可以更改headerLabel,但要注意的主要信息是,如果創建自己的視圖,則必須創建標簽並自己填寫文本。

當您實現該方法時,帶有標簽的默認視圖將被您返回的內容替換,因此,您需要添加帶有所需文本的標簽,這是您的任務。

暫無
暫無

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

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