首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS scrollView iOS

iOS scrollView iOS
EN

Stack Overflow用户
提问于 2014-03-21 07:44:58
回答 1查看 96关注 0票数 0

我对故事板中的UIScrollView有一个奇怪的问题。我创建了自定义UITableViewCell,并希望在其中包含UIScrollView。当我用代码做它的时候,它就像一种魅力。我就是这样做的:

代码语言:javascript
复制
- (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;
}

但是,当我在故事板中创建UIScrolView并放入我的原型单元格中时,将出口连接到customCell类,然后完全相同:

代码语言:javascript
复制
- (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;
}

我的手机是空的。你知道这是怎么回事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-21 08:04:31

- (instancetype)initWithCoder:(NSCoder *)aDecoder函数中,IBOutlets尚未与类连接。因此,当调用nil函数时,指针将具有- (void)awakeFromNib,它们将被连接。

所以你可以这样做。

代码语言:javascript
复制
- (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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22553009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档