【问题标题】:How to show table section titles in iOS 6?如何在 iOS 6 中显示表格部分标题?
【发布时间】:2014-01-09 23:53:30
【问题描述】:

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

这是相关代码。这是 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 中?

【问题讨论】:

  • self.displayAlphabet 的 items 数据类型是什么?
  • 您是否确保为tableView:heightForHeaderInSection: 返回一个非零高度,或者在表格视图上设置sectionHeaderHeight 属性?
  • 无论是本地存储还是从 db/some where 获取,如何获取字母顺序
  • @Ramshad NSMutableArray
  • 写入 NSLog(@"");在您的表委托中,并确保按预期返回值。

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


【解决方案1】:

您的方法调用顺序不是很清楚,因此您一定有一些错误。所以这里有一个简单的步骤指南。

声明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:

iOS7:

希望对你有帮助。

【讨论】:

    【解决方案2】:

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

    Apple 示例应用程序

    SimpleIndexedTableView

    参考文档:Apple Inc

    @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;
    }
    

    【讨论】:

      【解决方案3】:

      在要显示数据的 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
      

      返回标题视图;; }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-17
        • 1970-01-01
        • 2023-04-01
        • 2016-09-08
        • 1970-01-01
        相关资源
        最近更新 更多