Search code examples
iphoneobjective-cfor-loopaddsubview

Objective-C - addSubview In For Loop?


I have a for loop (see below) where I add my views to my view controller but when running my app, I am only seeing the first and last object be added to my view controller after running the loop. Its pretty simple, I just create the object and keep adding the next one below the one before it.

How come? Why else could I try? Thanks.

for (int i = 0; i < totalAddressCells; i++)
        {
            PersonDetailCell *cell = [[PersonDetailCell alloc] initWithFrame:CGRectMake(addressCell.frame.origin.x, (addressCell.frame.origin.y + addressCell.frame.size.height + cellSpacer) * (i +1), addressCell.frame.size.width, addressCell.frame.size.height)];

            [cell setBackgroundColor:[UIColor redColor]];

            [self.view addSubview:cell];
        }

Solution

  • The second argument to CGRectMake looks suspicious. Shouldn't that be

    addressCell.frame.origin.y + (addressCell.frame.size.height + cellSpacer) * (i+1)
    

    (different parentheses) ?