Setting accessibilityLabel for UITableView header text - ios

I am trying to find a way to set accessibilityLabel for my tableview's header text.
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Table View Header";
}
In my case, my accessibility label is different from the returned tableview's header text.

Alternatively, you could also use the textLabel property of UITableViewHeaderFooterView.
Code example:
-(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;
}

To set the accessibilityLabel property, write the text in the definition of the header view itself as I did in the code snippet hereunder by adding an accessibility element:
- (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;
}
Try this out and adapt it to your application.
Not that easy to go back to ObjC 🤯 but, now, you can set accessibilityLabel for UITableView header text by following this rationale. 🎉🎊🥳

Related

Footer overlays on top of tableview cell contents while scrolls

I have added footerview programmatically for each section as follows. I could able to see the footerview.
However, when I scroll up or down at the bottom or at the top of the tableview, footerview overlays on top of tableviewcells.
How could I able to disable it?
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(adminOrderElements[section].expanded && [adminOrderElements[section].notes length]>0)
{
return 60;
} else {
return 0;
}
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
return footer;
}
Before Scroll
After Scroll
If I understand your question correctly, you only have to change the UITableViewStyle to UITableViewStyleGrouped. According to the docs, for table views using grouped style the section headers and footers do not float.

cellForRowAtIndexPath equivalent function for section

I would like to dynamically change an image in my tableview custom section header cell once the image finished being downloaded. However I do not how to get the reference to the section header cell. Is there an equivalent function for getting the header cell like there is for getting the table view cell using the function cellForRowAtIndexPath?
I tried but this is for the row. I need an equivalent for section header
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as? ListingTableViewCell {
cellToUpdate.imageView.image = image!
}
Just set tags for section header view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [UIView new];
view.backgroundColor = [UIColor orangeColor];
view.tag = section;
return view;
}
Then use tag to get the section header view.
NSInteger section2 = 2;
UIView *sectionHeader = [self.tableView viewWithTag:section2];
To customize your headers, you can override the following method from the UITableViewDataSource.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
It works pretty much cellForRowAtIndexPath. You juste have to create your view and return it. Here is an example to set a UIImage as header of section 0 :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
switch (section) {
case 0:
imageView.image = [UIImage imageNamed:#"image"];
return imageView;
break;
default:
break;
}
return [[UIView alloc] init];
}

How to change index based section common color in UITableView

Normally my screen display is like this:
When I use this code
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor whiteColor]];
return headerView;
}
my output is like this:
My section index text is not visible. I know that the section header view add my custom view. I want to change section header color and also display index text.
#Khawar answer i get this output
this is work 100% and you can change your index or header text color
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
view.tintColor = [UIColor blueColor];
// if you have index/header text in your tableview change your index text color
UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
[headerIndexText.textLabel setTextColor:[UIColor blackColor]];
}
output:
Try to set the height for UITableView header.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
If you only need to change the background color of section header while displaying section title, you don't need to create custom view for that. Custom view would overwrite your default header and your title would not be shown, unless you add custom UILabel in your custom view. Just use following UITableView delegate to change background color.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
view.tintColor = [UIColor redColor];
}
See results before and after.
----------------Before------------------
----------------After------------------
Hope this fixes the issue.
You have to give title for section... Or you can use this method..
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
headerView.text = [self tableView:tableView titleForHeaderInSection:section];
return headerView;
}

How can I apply this header/footer margin between UITableViewCells?

This code creates a margin at the top and bottom of the UITableView. How can I modify it to do the same but between each individual UITableViewCell?
// set header height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
// set header colour
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
// set footer height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
// set footer colour
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor lightGrayColor];
return headerView;
}
If successful, I'd also like to be able to add similar left and right margins if possible.
Use an extra Background UIView with top, left. bottom and right margin.[red background view in the picture]. Add all other subViews on this view.

Why is my first section header in table not showing?

I can't figure out why the first section header isn't showing. The second and third show fine. I suspect it's because of the search bar.
I've tried offsetting the whole table like in UISearchBar covered by section header but I didn't notice it offset.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
// create the label object
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.frame = CGRectMake(0,0,self.view.frame.size.width,60);
UIColor* mainColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];
headerLabel.backgroundColor = mainColor;
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.textAlignment = UITextAlignmentCenter;
if(section == 0)
headerLabel.text = #"Friends";
if(section == 1)
headerLabel.text = #"Second Section Header";
if(section == 2)
headerLabel.text = #"Third Section Header";
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
- (void) hideSearchBar
{
self.feedTableView.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [_imageDataArray objectAtIndex:section];
NSArray *array = [dictionary objectForKey:#"data"];
return [array count];
}
It wasn't showing because i hadn't implemented heightForHeaderInSection
More details here:
in iOS 7 viewForHeaderInSection section is starting from 1 not from 0
You got a bug in your code. Your header is there but it is setting at a y = -60 with a height of 60.
Replace :
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
with This
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,300,60)];
Here is the screen shot here
Hope that helps.
In my case I needed to explicitly assign delegates in code, even though delegates were already connected in the XIB file.
tableView.delegate = self
tableView.dataSource = self

Resources