将多个UIViews动态加载到UIScrollView中

时间:2012-08-31 07:30:57

标签: uiview uiscrollview

我试图动态地将多UIViews加载到UIScrollView - 就像Facebook上的帖子一样。但我不太明白你如何动态地定位UIViews。有人可以给我一些建议吗?

1 个答案:

答案 0 :(得分:0)

我在网络上找到了一个循环方法。想要感谢消息来源,但我忘了从哪里得到它:

- (void)constructPosts:(NSArray*)listOfItems
{
    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, scrollView.frame.size.height*[listOfItems count])];
    CGRect frame = CGRectMake(10, 10, 300, 200);
    for (int i = 0; i < [listOfItems count]; i++)
    {
        UIView *aView = [[UIView alloc] init];
        UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 20)];
        [test setUserInteractionEnabled:YES];
        test.text = [NSString stringWithFormat:@"%@",[listOfItems objectAtIndex:i]];
        NSLog(@"setting up gestures");



        [aView addSubview:test];


        [aView setBackgroundColor:[UIColor grayColor]];
        frame.origin.y=((frame.size.width+5)*i+5 + (i/3) * 5);
        aView.frame = frame;
        [scrollView addSubview:aView];
    }

}