简体   繁体   中英

UITextView in UIScrollView both inside another UIScrollView

I'm adding a series of text views inside scroll views to a scroll view. I can draw the first text view in its scroll view. When I attempt to add the second text view the associated scroll view is drawn bit the text view is not added. Can anyone point out what I'm doing wrong here?

for (int i = 0; i < numberOfViews; i ++) {

    CGFloat xOrigin = i * 230;
    CGRect textRect = CGRectMake(xOrigin, 0, 210, 250);
    CGRect imageRect = CGRectMake(xOrigin, 30, 210, 5);
    CGRect subscrollRect = CGRectMake(xOrigin, 40, 210, 250);

    _image1 = [[UIImageView alloc] initWithFrame:imageRect];
    _image1.image = [UIImage imageNamed:@"horizontal_line_02.png"];

    if (i == 0) {
        _textView1 = [[UITextView alloc] initWithFrame:textRect];
        _textView1.delegate = self;
        _textView1.userInteractionEnabled = NO;
        _textView1.text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
        _subScrollView1 = [[UIScrollView alloc] initWithFrame:subscrollRect];
        [_subScrollView1 setContentSize:CGSizeMake(210, 500)];
        [_subScrollView1 addSubview:_textView1];
        [_textScrollView addSubview:_subScrollView1];
        [_textScrollView addSubview:_image1];
    }
    else if (i == 1) {
        _textView2 = [[UITextView alloc] initWithFrame:textRect];
        _textView2.delegate = self;
        _textView2.userInteractionEnabled = YES;
        _textView2.text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent accumsan elementum tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Integer sapien turpis, laoreet id ullamcorper sed, accumsan id ligula. Etiam euismod, augue sed porta eleifend, mauris dolor scelerisque velit, ac facilisis libero elit et arcu. Morbi ligula libero, porta id leo ut, convallis adipiscing enim.";
        _subScrollView2 = [[UIScrollView alloc] initWithFrame:subscrollRect];
        [_subScrollView2 setContentSize:CGSizeMake(210, 500)];
        [_subScrollView2 addSubview:_textView2];
        [_textScrollView addSubview:_subScrollView2];
        [_textScrollView addSubview:_image1];
    }

It seems you use the same textField2 everytime. (so, basically you're releasing it every iteration, which should show you the last one.

instead of:

_textView2 = [[UITextView alloc] initWithFrame:textRect]; 

try :

UITextView *textView2 = [[UITextView alloc] initWithFrame:textRect]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM