作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我能够在 iOS 7 中的表格部分显示标题,但文本不会出现在 iOS 6 中。我知道你可以使用标题或自定义标题 View ,但不能同时使用,所以我做了确保不要定义 tableView:viewForHeaderInSection:
。
这是相关代码。这是 UIViewController 的子类,而不是 UITableViewController。我需要一个粘性表格标题,所以我必须对表格 View 进行自定义组合。
- (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 中?
最佳答案
您一定是遇到了一些错误,因为您的方法调用顺序不是很清楚。所以这是一个简单的步骤指南。
声明一个 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"];
现在,很少有委托(delegate)方法,
-(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:
希望对您有所帮助。
关于ios - 如何在 iOS 6 中显示表格部分标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033671/
我是一名优秀的程序员,十分优秀!