如何在iOS 6中显示表格部分标题?

时间:2014-01-09 23:53:31

标签: ios iphone objective-c cocoa-touch uitableview

我能够在iOS 7中的表格部分显示标题,但文字没有出现在iOS 6中。我知道你可以使用标题或自定义标题视图,但不能同时使用两者,所以我制作了一定不要定义tableView:viewForHeaderInSection:

enter image description here enter image description here

这是相关代码。这是UIViewController的子类,而不是UITableViewController。我需要一个粘性表头,所以我不得不自定义组合表视图。

- (void)loadView
{
    [super loadView];

    // "width" and "height" properties are convenience methods
    // that I defined in a UIView category.
    self.tableView = [[UITableView alloc]
        initWithFrame:CGRectMake(
            0.0,
            SEARCH_BAR_HEIGHT + self.avatarCollectionView.height,
            self.view.width,
            self.view.height - (SEARCH_BAR_HEIGHT + self.avatarCollectionView.height)
        )
        style:UITableViewStylePlain
    ];
    [self.view addSubview:self.tableView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // displayAlphabet is an NSMutableArray of one character strings.
    // It is a subset of the entire alphabet. For example, it can be:
    // @[@"A", @"B", @"C", @"E", @"X", @"Z"]
    return self.displayAlphabet.count;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.displayAlphabet objectAtIndex:section];
}

- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.displayAlphabet;
}

如何让标题文字显示在iOS 6中?

3 个答案:

答案 0 :(得分:4)

您必须遇到一些错误,因为您的方法调用顺序不是很清楚。所以这是一个简单的步骤指南。

声明NSArray *titleArray来保存你的头衔。在viewDidLoad中初始化您的表格以及您的标题数组。

tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableView.delegate         = self;
tableView.dataSource       = self;
tableView.backgroundColor = [UIColor whiteColor];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tableView];

titleArray = @[@"A", @"B", @"C", @"E", @"X", @"Z"];

现在,很少有委托方法,

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [titleArray objectAtIndex:section];
}

- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   return titleArray;
}

这是输出。

iOS6:

iOS6 Screen Shot

ioS7:

ios7 screen Shot

我希望有所帮助。

答案 1 :(得分:0)

UILocalizedIndexedCollation类可以方便地为具有节索引的表视图组织,排序和本地化数据。然后,表视图的数据源使用排序规则对象为表视图提供节标题和节索引标题的输入。

Apple Sample App

SimpleIndexedTableView

Referance Doc:Apple Inc

enter image description here

enter image description here

@property (nonatomic) NSMutableArray *sectionsArray;
@property (nonatomic) UILocalizedIndexedCollation *collation;

-(void)viewDidLoad{

 [self configureSections];
 [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[self.collation sectionTitles] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Check to see whether the normal table or search results table is being displayed and return the count from the appropriate array

    NSArray *timeZonesInSection = (self.sectionsArray)[section];

    NSLog(@"count tableview %d",timeZonesInSection.count);

    return [timeZonesInSection count];

}

/*
 Section-related methods: Retrieve the section titles and section index titles from the collation.
 */

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.collation sectionTitles][section];
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.collation sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [self.collation sectionForSectionIndexTitleAtIndex:index];
}


- (void)configureSections {


    // Get the current collation and keep a reference to it.
    self.collation = [UILocalizedIndexedCollation currentCollation];

    NSInteger index, sectionTitlesCount = [[self.collation sectionTitles] count];

    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

    // Set up the sections array: elements are mutable arrays that will contain the time zones for that section.
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
    }


    self.sectionsArray = newSectionsArray;
}

答案 2 :(得分:0)

在要显示数据的UITableView类中使用以下代码 - :

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{
NSString *title = nil;
switch (section) 
{
    case section1:
        title= @"First";
        break;
    case section2:
        title= @"Second";
        break;
    case section3:
        title = @"Third";
        break;
    default:
        break;
}

return title;

}

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

{

//    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0,100, 320, 44.0)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, -28.0, 300.0, 90.0)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.font = [UIFont boldSystemFontOfSize:15];
headerLabel.textColor = [UIColor whiteColor];
    //headerView.contentMode = UIViewContentModeScaleToFill;

    // Add the label
    switch (section)
    {
        case section1:
        {                
            headerLabel.text = @"First";

            [headerView addSubview: headerLabel];
            break;
        }
        case section2:
        {                
            headerLabel.text = @"Second";

            [headerView addSubview: headerLabel];
            break;
        }

        case section3:
        {
            headerLabel.text = @"Third";

            [headerView addSubview: headerLabel];
            break;
        }


        default:
            break;
    }


    // Return the headerView

return headerView ;; }