UITableViewCell subtype returns only UITableViewCellScrollView in iOS7

I have a custom UITableViewCell with 7 subzones. One of them is a type of activity, so in order to find it and stop it, I do something like this:

 NSArray *subviews=[cell subviews]; NSLog(@"Subviews count: %d",subviews.count); for (UIView *view in subviews) { NSLog(@"CLASS: %@",[view class]); // code here } 

In iOS6 , Subviews: 7 counts, and one of them is a kind of activity. But in iOS7, counting Subviews: 1 and [view class] returns a UITableViewCellScrollView . Tried, NSArray *subviews=[cell.superview subviews]; and NSArray *subviews=[cell.contentview subviews]; but in vain.

Any suggestions?

+6
source share
5 answers

You need to recursively descend into each subview subheading and so on. Never make any assumptions about the private structure of subview. Even better, since you should only add subviews to the contentView cell, just look at the contentView , not the whole cell.

+13
source

I think you should add a conditional statement to your code:

 NSArray *subviews; if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) subviews = aCell.contentView.subviews; else subviews = aCell.subviews; for(id aView in subviews) { if([aView isKindOfClass:[aField class]]) { //your code here } } //don't forget to add a conditional statement even on adding your subviews to cell if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) [aCell.contentView addSubview:aField]; else [aCell addSubview:aField]; 

This is the definition for the above SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO macro:

 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 
+1
source

I dynamically added imageview to the cell, so for self.contentview.subviews separator string and accessory were removed. So I did something like

 for (id obj in self.contentView.superview.subviews) { if ([obj isMemberOfClass:[UIImageView class]]) { [obj removeFromSuperview]; } } 

worked for me, hope few people will work!

+1
source

Here's how to iterate over controls inside a custom UITableViewCell

 UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"anyname"]; UIView *view=cell.contentView; for(id object in view.subviews) { if([object isKindOfClass:[UILabel class]]) { NSLog(@"%@",[object class]); // UILabel *label= (UILabel*)view; //[label setFont:TextFont]; } } 

You can test any control based on the class of that particular control.

0
source

Since I did not see the right answer to this question then, I would post it here, even if he is already a year old. (Credit to Brian Nickel, who has already posted about this). First of all, you get only one view, because the cell has a contentView where all its children live (this has already been explained). Regarding how to find your view inside the contentView , in this Apple documentation https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html you can see what they recommend use viewWithTag: to view the views inside the content view. So you need to mark each view in your content view, or just the ones you want to find, and then call: [cell.contentView viewWithTag: tag] Hope this helps any googlers like me.

0
source

Source: https://habr.com/ru/post/955229/


All Articles