AYMADA
AYMADA

Reputation: 899

iOS scrollView inside UITableCellView

I have a strange issue with UIScrollView in storyboard. I created custom UITableViewCell and I want to have UIScrollView inside it. When I do it in code it works like a charm. I do it like that:

- (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;
}

but when I create UIScrolView in storyboard and put in in my prototype cell, connect outlet with customCell class and then to exactly the same like that:

- (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;
}

my cell is empty. Do you know what is wrong with it?

Upvotes: 0

Views: 97

Answers (1)

Srikanth
Srikanth

Reputation: 1930

In - (instancetype)initWithCoder:(NSCoder *)aDecoderfunction, IBOutlets are not connected with your class yet. So the pointers will have nil They will be connected when - (void)awakeFromNib function is called.

So you can do like this.

- (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;
}

Upvotes: 1

Related Questions