UITableCellView中的iOS scrollView

时间:2014-03-21 07:44:59

标签: ios uitableview uiscrollview

我在故事板中遇到UIScrollView的奇怪问题。我创建了自定义UITableViewCell,我希望在其中包含UIScrollView。当我在代码中执行它时,它就像一个魅力。我这样做:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 25, 650, 25)];
        scroller.showsHorizontalScrollIndicator = NO;

        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
        contentLabel.backgroundColor = [UIColor blackColor];
        contentLabel.textColor = [UIColor whiteColor];
        NSMutableString *str = [[NSMutableString alloc] init];
        for (NSUInteger i = 0; i < 100; i++)
        {
            [str appendFormat:@"%i ", i];
        }
        contentLabel.text = str;

        [scroller addSubview:contentLabel];
        scroller.contentSize = contentLabel.frame.size;
        [self addSubview:scroller];

    }

    return self;
}

但是当我在storyboard中创建UIScrolView并放入我的原型单元格时,将outlet与customCell类连接起来,然后完全相同:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        storyboardScrollView.showsHorizontalScrollIndicator = NO;

        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
        contentLabel.backgroundColor = [UIColor blackColor];
        contentLabel.textColor = [UIColor whiteColor];
        NSMutableString *str = [[NSMutableString alloc] init];
        for (NSUInteger i = 0; i < 100; i++)
        {
            [str appendFormat:@"%i ", i];
        }
        contentLabel.text = str;

        [storyboardScrollView addSubview:contentLabel];
        storyboardScrollView.contentSize = contentLabel.frame.size;
}

    return self;
}

我的手机是空的。你知道它有什么问题吗?

1 个答案:

答案 0 :(得分:1)

- (instancetype)initWithCoder:(NSCoder *)aDecoder函数中,IBOutlets尚未与您的班级相关联。因此指针将nil当调用- (void)awakeFromNib函数时它们将被连接。

所以你可以这样做。

- (void)awakeFromNib
{
    storyboardScrollView.showsHorizontalScrollIndicator = NO;

    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
    contentLabel.backgroundColor = [UIColor blackColor];
    contentLabel.textColor = [UIColor whiteColor];
    NSMutableString *str = [[NSMutableString alloc] init];
    for (NSUInteger i = 0; i < 100; i++)
    {
        [str appendFormat:@"%i ", i];
    }
    contentLabel.text = str;

    [storyboardScrollView addSubview:contentLabel];
    storyboardScrollView.contentSize = contentLabel.frame.size;
}