Ios 为UITableView标题文本设置accessibilityLabel

Ios 为UITableView标题文本设置accessibilityLabel,ios,objective-c,uitableview,uiaccessibility,Ios,Objective C,Uitableview,Uiaccessibility,我正在尝试为我的tableview标题文本设置accessibilityLabel -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Table View Header"; } 在我的例子中,我的可访问性标签与返回的tableview的标题文本不同。要设置accessibilityLabel属性,在标题视图本身的定义中

我正在尝试为我的tableview标题文本设置
accessibilityLabel

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {        
    return @"Table View Header";
}

在我的例子中,我的可访问性标签与返回的tableview的标题文本不同。

要设置
accessibilityLabel
属性,在标题视图本身的定义中写入文本,就像我在下面的代码段中添加可访问性元素一样:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {        
    return @"Table View Header";
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    CGRect headerViewFrame = CGRectMake(0.0, 0.0, tableView.frame.size.width, 44.0);
    UIView * headerView = [[UIView alloc] initWithFrame:headerViewFrame];

    UILabel * headerViewLabel = [[UILabel alloc] initWithFrame:headerViewFrame];
    headerViewLabel.text = @"Table View Header";
    [headerView addSubview: headerViewLabel];

    UIAccessibilityElement * a11yHeader = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:headerView];
    a11yHeader.accessibilityFrameInContainerSpace = headerView.frame;
    a11yHeader.isAccessibilityElement = YES;
    a11yHeader.accessibilityLabel = @"my new header text for accessibility";
    a11yHeader.accessibilityTraits = UIAccessibilityTraitHeader;

    headerView.accessibilityElements = @[a11yHeader];

    return headerView;
}
尝试一下,并根据您的应用程序进行调整


返回ObjC并非易事或者,您也可以使用
UITableViewHeaderFooterView
textLabel
属性

代码示例:

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

    UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] init];
    headerView.textLabel.text = @"tableview header";
    headerView.isAccessibilityElement = YES;
    headerView.accessibilityLabel = @"tableview header for accessibility";

    return headerView;
}