gpt4 book ai didi

UITableCellView 内的 iOS scrollView

转载 作者:行者123 更新时间:2023-11-29 03:07:55 24 4
gpt4 key购买 nike

Storyboard中的 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 并将其放入我的原型(prototype)单元格中时,将 socket 与 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;
}

我的单元格是空的。你知道它有什么问题吗?

最佳答案

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

关于UITableCellView 内的 iOS scrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22553009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com