我的表格 View 中的每个单元格内都有一个 ScrollView ,尽管嵌入 ScrollView 中的图片只有在我上下滚动表格 View 后才会显示出来...... 我有一个带有 @property 的自定义类,用于 ScrollView 和单元格。我有用于在 cellForRowAtIndexPath: 方法中设置 ScrollView 的代码。这也应该在细胞的初始创建时调用吗?我很困惑。
如何解决该问题并使图像在启动应用程序时首先出现?
相关代码:
- (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;
}
最佳答案
这与您的问题并不完全相关,但您也会遇到这个问题:
不要忘记您的细胞将被重复使用。重用时(假设用户向下滚动,一个单元格向上移出屏幕,底部出现的下一个单元格实际上是刚刚消失并被重用的 CustomCell 实例。)
因此添加:
for (UIView *aView in [NSArray arrayWithArray:cell.subviews]) {
[aView removeFromSuperview];
// if you don't arc then release them and take care of their subviews - if any.
}
在添加任何新的 UIImageView 之前。 (我以为有一种方法可以一次性删除所有 subview 但没有找到)
关于ios - UIScrollView中UITableView内容问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906796/