【发布时间】:2013-07-28 09:24:36
【问题描述】:
我的表格视图的每个单元格内都有一个滚动视图,尽管在我上下滚动表格视图之前,嵌入在我的滚动视图中的图片不会显示... 我有一个带有滚动视图和单元格的@property 的自定义类。我有在 cellForRowAtIndexPath: 方法中设置滚动视图的代码。这也应该在最初创建单元格时调用?我很困惑。
我怎样才能摆脱这个问题,让我在启动应用程序时首先出现图像?
相关代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"CustomID";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
for (int i = 0; i < [imageArray count]; i++) {
CGRect frame;
frame.origin.x = cell.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = cell.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
[cell.scrollView addSubview:imageView];
}
cell.scrollView.contentSize = CGSizeMake(cell.scrollView.frame.size.width * [imageArray count], cell.scrollView.frame.size.height);
return cell;
}
【问题讨论】:
-
抱歉,您有什么问题?
-
你在哪里实例化滚动视图?您是否在 IB/Storyboard Editor 中设计了
CustomCell? -
单元格和滚动视图的默认大小是多少 - 框架是否等于
CGRectZero? -
这取决于您创建它的位置和方式。如果你没有设置任何框架而不是 CGRectZero。但这不太可能。那么它们是在哪里以及如何创建的?国际文凭组织?以编程方式在单元格的 init/initWithStyle: 方法中?
-
您应该在 CustomCell 中添加 imageView 作为属性。在 initWithStyle 中分配它。然后您将仅在 cellForRowAtIndexPath 中添加图像。
标签: ios uitableview uiscrollview