Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell subviews returns only UITableViewCellScrollView in iOS7

I have a custom UITableViewCell with 7 subviews. One of them is an activity view, so in order to find it and stop, 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 count: is 7 and one of them is the activity view. But in iOS7 , the Subviews count: is 1 and [view class] returns UITableViewCellScrollView . Tried, NSArray *subviews=[cell.superview subviews]; and NSArray *subviews=[cell.contentview subviews]; , but in vain.

Any suggestions?

like image 722
DD_ Avatar asked Oct 04 '13 05:10

DD_


2 Answers

You need to recursively descend into each subview's subviews and so on. Never make any assumptions about the private subview structure. Better yet, since you should only add subviews to the cell's contentView, just look in the contentView, not the whole cell.

like image 72
rmaddy Avatar answered Oct 07 '22 06:10

rmaddy


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 define 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)
like image 33
valvoline Avatar answered Oct 07 '22 06:10

valvoline