Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all subviews for a UIScrollView

I need to get an array of all the subviews in a UIScrollView. Right now I'm using

NSArray *subviews = [myScrollView subviews];

but this seems to only be returning the subviews that are visible at the time the code is run. I need all the subviews in the whole extent of the UIScrollView, even those that are currently hidden (as in off screen). How would I get that?

Essentially, I'm looking for something like the contentSize property of a UIScrollView, except instead of returning just the size of the UIScrollView if it were big enough to display all of it's content, I want it to return the content itself.

EDIT: I think I've figured it out: the scroll view this isn't working for is actually a UITableView - and I think it's deque-ing the cells that are off screen on me, and that's why they aren't showing up. I'm going to do some testing to confirm.

like image 537
GeneralMike Avatar asked Dec 21 '22 00:12

GeneralMike


2 Answers

Try with following code its working for me.

for(UIView * subView in myScrollView.subviews ) // here write Name of you ScrollView.
{ 
     // Here You can Get all subViews of your myScrollView.
    // But For Check subview is specific UIClass such like label, button, textFiled etc.. write following code (here checking for example UILabel class).

        if([subView isKindOfClass:[UILabel class]]) // Check is SubView Class Is UILabel class?
        {
            // You can write code here for your UILabel;
        }
}
like image 64
iPatel Avatar answered Dec 24 '22 09:12

iPatel


tl;dr

It turns out that

NSArray *subviews = [myScrollView subviews];

will indeed return all the subviews in a UIScrollView *myScrollView, even if they are off-screen.


The Details

The problem I was actually having was that the scroll view I was trying to use this on was actually a UITableView, and when a UITableViewCell in a UITableView goes off-screen, it actually gets removed from the UITableView - so by the time I was calling subviews, the cells I was looking for were no longer in the scroll view.

My workaround was to build all of my UITableViewCells in a separate method called by my viewDidLoad, then put all of those cells into an array. Then, instead of using subviews, I just used that array. Of course, doing it this way hurts the performance a little (in cellForRowAtIndexPath you just return the cell from the array, which is slower than the dequeueReusableCellWithIdentifier method that is typically used), but it was the only way I could find to get the behavior I needed.

like image 22
GeneralMike Avatar answered Dec 24 '22 09:12

GeneralMike