erdemgc
erdemgc

Reputation: 125

how to iterate over dictionary?

I have a dictionary, in this dictionary, i have arrays. in these arrays, i have views which consist of subviews. One of these subview is a uilabel, i need to reach that uilabel.

When i try to iterate over it, i take an error "collection expression type uiview may not respond to countbyenumaretingwithstate:objects:count"

my code is as below;

for(int i = 0; i<counterRow;i++)
{
    NSString *dictTempKey = [NSString stringWithFormat:@"row%d",i];

  NSMutableArray * tempArray =  [doctorAddition objectForKey:dictTempKey];

    for(UIView * subview in tempArray)
    {

        UIView * temp = subview;

        for(UIView * subview2 in (UIView*)temp)
        {
            if([subview2 isKindOfClass:[UILabel class]])
            {

            }

        }

    }

}

Upvotes: 0

Views: 175

Answers (3)

Lokesh Chowdary
Lokesh Chowdary

Reputation: 736

// if a view is having more sub view(i.e., UILable, imageview, UIButton..etc), Now how to identify a particular view.. Below Code is the best example..

// This is a general example, this will help full for all users

for (UIView *subview_obj in [View_obj subviews])// Here it ll fetch all subview from view
{
   if ([subview_obj isKindOfClass:[UIButton class]])// here it ll identify perticular view from all subview
   {
     // Do Something
   }
}

Upvotes: 0

Javi Campa&#241;a
Javi Campa&#241;a

Reputation: 1481

I think that you missing this (UIView*)temp.subviews, in the third for:

for(int i = 0; i<counterRow;i++)
{
    NSString *dictTempKey = [NSString stringWithFormat:@"row%d",i];

  NSMutableArray * tempArray =  [doctorAddition objectForKey:dictTempKey];

    for(UIView * subview in tempArray)
    {

        UIView * temp = subview;

        for(UIView * subview2 in (UIView*)temp.subviews)
        {
            if([subview2 isKindOfClass:[UILabel class]])
            {

            }

        }

    }

}

Upvotes: 3

sbarow
sbarow

Reputation: 2819

You could do the following....

NSArray *values = [doctorAddition allValues];

[values enumerateObjectsUsingBlock:^(UIView *aView, NSUInteger index, BOOL *stop) {

    [aView.subviews enumerateObjectsUsingBlock:^(id aSubView, NSUInteger index, BOOL *stop) {

        if ([aSubView isKindOfClass:[UILabel class]]) {
            NSLog(@"Found UILabel: %@", aSubView);
        }
    }];
}];

Upvotes: 1

Related Questions