ScrollView以编程方式在uitableview中添加

时间:2017-05-02 13:56:15

标签: ios uiscrollview tableview

我在tableview单元格中添加了scrollview,但在iphone 5或5s上,imageview右侧还有一些小空间。我添加滚动视图而不是滚动视图内部还有另一个uiview,在uiview中有一个imageview。enter image description here

    UIScrollView * riderScroll = [[UIScrollView alloc] initWithFrame:frame];

    riderScroll.clipsToBounds = YES;
    riderScroll.delegate = self;
    riderScroll.pagingEnabled = YES;
    [riderScroll setShowsHorizontalScrollIndicator:YES];
    [riderScroll setShowsVerticalScrollIndicator:NO];
    NSLog(@"%@",NSStringFromCGRect(cell.contentView.frame));
    NSLog(@"%@",NSStringFromCGRect(riderScroll.frame));
    NSLog(@"%@",NSStringFromCGRect(cell.outerView.frame));
    for (int i=0; i<obj.propertyImages.count; i++) {

        float swipeWidth = screenWidth;
        int x = i * swipeWidth;

        UIView * insideView=[[UIView alloc]initWithFrame:CGRectMake(x, riderScroll.frame.origin.y, screenWidth, riderScroll.frame.size.height)];

        insideView.clipsToBounds = YES;
        NSLog(@"%@",NSStringFromCGRect(insideView.frame));
        UIImageView * img = [[UIImageView alloc] initWithFrame:CGRectMake(0, insideView.frame.origin.y,  insideView.frame.size.width, insideView.frame.size.height)];

        NSLog(@"%@",NSStringFromCGRect(img.frame));

        img.clipsToBounds = YES;
        img.userInteractionEnabled = YES;
        img.contentMode = UIViewContentModeScaleAspectFill;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [tap  addTarget:self action:@selector(tapOnImage:)];
        [img addGestureRecognizer:tap];
        img.tag = indexPath.row;
        PropertyObject * imgObj = [obj.propertyImages objectAtIndex:i];
        NSString *imageUrl;

        if (_isMyProperty || _isAllProperty) {
            imageUrl = [NSString stringWithFormat:@"%@/%@",BASE_URL_IMAGE,imgObj.imgUrl];
        }else{
            imageUrl = [NSString stringWithFormat:@"%@/%@",BASE_URL_IMAGE,imgObj];
        }

        imageUrl = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [img setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@""]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {

            }

        } usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray

         ];

        [insideView addSubview:img];
        [riderScroll addSubview:insideView];
        [cell.outerView addSubview:riderScroll];
        NSLog(@"%@",NSStringFromCGRect(cell.outerView.frame));
        NSLog(@"%@",NSStringFromCGRect(riderScroll.frame));
        NSLog(@"%@",NSStringFromCGRect(insideView.frame));
        NSLog(@"%@",NSStringFromCGRect(img.frame));
        float maxContentSize = swipeWidth * obj.propertyImages.count;
        riderScroll.contentSize = CGSizeMake(maxContentSize, cell.outerView.bounds.size.height);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这样的细胞类:(未经测试可以给你一个想法)

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.outerview setFrame:CGRectMake(0, 0, screenSize.width, self.outerview.frame.size .height)];
     ...
     ... Similarly resize rider scrollview and its nested views
     ... each one at a time and see when the required view 
     ... fits your need.
     ...
}