将UIScrollView放在CollectionViewCell上但滚动视图无法滚动

时间:2015-05-28 07:24:44

标签: ios iphone uiscrollview uicollectionview

我在UIScrollView上放了CollectionViewCell,但scrollview无法滚动

#import <UIKit/UIKit.h>


@interface MyCollectionViewCell : UICollectionViewCell<UIScrollViewDelegate>{

    IBOutlet UIScrollView *toolsScrollView;


}


@end

@implementation MyCollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //wangzheng
        self = [super initWithFrame:frame];
        if (self) {
            NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"MyCollectionViewCell"owner:self options:nil];
            UIView *bw = [nibView objectAtIndex:0] ;
            [self.contentView addSubview:bw];
            toolsScrollView.delegate=self;


            UIView *toolView1 = [[UIView alloc] init];
            toolView1.backgroundColor = [UIColor blackColor];
            toolView1.frame = CGRectMake(300, 0, 80, 150);
            [toolsScrollView addSubview:toolView1];


            toolsScrollView.delegate=self;
            [toolsScrollView setContentSize:CGSizeMake(400 * 2, toolsScrollView.frame.size.height)];
            toolsScrollView.showsVerticalScrollIndicator = NO;
            toolsScrollView.showsHorizontalScrollIndicator = NO;
            toolsScrollView.delegate = self;
            toolsScrollView.userInteractionEnabled=true;
            toolsScrollView.scrollEnabled=true;
        }
        return self;
    }
    return self;
}

IBOutlet UIScrollView *toolsScrollView指向XIB中的UIScrollView

enter image description here

滚动视图无法滚动且函数scrollViewDidScroll永远不会被触发

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    return;

}

1 个答案:

答案 0 :(得分:0)

可能的问题:

  1. toolsScrollView变量未连接且未指向有效的scrollview或
  2. 从nib加载后,toolsScrollView的框架不正确。或者,
  3. 3.它实际上是滚动但不可见,因此为了测试目的,启用了以下内容 -

    toolsScrollView.showsVerticalScrollIndicator = YES;
    toolsScrollView.showsHorizontalScrollIndicator = YES;