How do I disable access to configure static UITableViewCell

I have a storyboard containing UITableViewController static content. The cells are very simple, containing only one UILabel . Now, if I want to disable access to one of the cells, I'll just take off a mark on the label. This works as expected.

However, if now I have to create an empty subclass UITableViewCell and use it as a cell class for my static cell availability is turned on, ignoring all settings.

I tried to override -isAccessibilityElement , to return NO , the software setting for all child views accessibilityElement property NO , but it will still be available when using VoiceOver. The content will not be read VoiceOver, only one "," there seems to be (can be heard when scrolling up / down in this element).

What do I need to disable access to my custom cell?

+3
source share
3 answers

Perhaps this is easier.

 cell.textLabel.accessibilityElementsHidden = YES; 

See this post

;)

+5
source

Well, I found a solution, although I'm not very happy about that.

To turn off the cell as the availability of an item, you need to turn it into a disposable container with no elements:

 @implementation CustomCell - (BOOL)isAccessibilityElement { return NO; // prerequisite for being an accessibility container } - (NSInteger)accessibilityElementCount { return 0; // hack to disable accessibility for this cell } - (id)accessibilityElementAtIndex:(NSInteger)index { return nil; } - (NSInteger)indexOfAccessibilityElement:(id)element { return NSNotFound; } @end ) index { @implementation CustomCell - (BOOL)isAccessibilityElement { return NO; // prerequisite for being an accessibility container } - (NSInteger)accessibilityElementCount { return 0; // hack to disable accessibility for this cell } - (id)accessibilityElementAtIndex:(NSInteger)index { return nil; } - (NSInteger)indexOfAccessibilityElement:(id)element { return NSNotFound; } @end ) element { @implementation CustomCell - (BOOL)isAccessibilityElement { return NO; // prerequisite for being an accessibility container } - (NSInteger)accessibilityElementCount { return 0; // hack to disable accessibility for this cell } - (id)accessibilityElementAtIndex:(NSInteger)index { return nil; } - (NSInteger)indexOfAccessibilityElement:(id)element { return NSNotFound; } @end 
+3
source

The Swift

* Sample code - this Swift 3, but the key line of code to set the accessibilityElementsHidden is not specific to the Swift 3.

Before displaying cells (UITableViewCell) you should set the property accessibilityElementsHidden in true . This property indicates whether the accessibility elements contained in the accessibility element (in this case a cell) are hidden. accessibilityElementsHidden defaults false .

Inside init ()

The following code sets accessibilityElementsHidden true to initialize the user subclass UITableViewCell. This will work if the cell is created raskadroy, the nib or created software.

 class CustomTableViewCell: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier) self.accessibilityElementsHidden = true } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.accessibilityElementsHidden = true } } : String?) { class CustomTableViewCell: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier) self.accessibilityElementsHidden = true } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.accessibilityElementsHidden = true } } reuseIdentifier) class CustomTableViewCell: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier) self.accessibilityElementsHidden = true } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.accessibilityElementsHidden = true } } 

Inside awakeFromNib ()

If CustomTableViewCell will be created only from the storyboard or nib, you can set the property in awakeFromNib() .

 class CustomTableViewCell: UITableViewCell { override func awakeFromNib() { self.accessibilityElementsHidden = true } } 

Inside tableView (_: cellForRowAt :)

If you created and overrides the cell programmatically, the code is as follows:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // ... code that creates or dequeues the cell cell.accessibilityElementsHidden = true return cell } cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // ... code that creates or dequeues the cell cell.accessibilityElementsHidden = true return cell } cell func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // ... code that creates or dequeues the cell cell.accessibilityElementsHidden = true return cell } 
+1
source

All Articles