gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:05:13 25 4
gpt4 key购买 nike

我能够在 iOS 7 中的表格部分显示标题,但文本不会出现在 iOS 6 中。我知道你可以使用标题或自定义标题 View ,但不能同时使用,所以我做了确保不要定义 tableView:viewForHeaderInSection:

enter image description here enter image description here

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

iOS6 Screen Shot

iOS7:

ios7 screen Shot

希望对您有所帮助。

关于ios - 如何在 iOS 6 中显示表格部分标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com