ios - UIScrollView中UITableView内容问题

标签 ios uitableview uiscrollview

我的表格 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/

相关文章:

ios - 在 UITableView 加载数据之前,MasterViewController 中添加的 View 不可见

ios - 监控两个位置之间的距离

ios - 检测 UILabel 中的点击标签

ios - 当应用程序进入前台时不要恢复 MPMoviePlayerController

ios - UITableView 不滚动,根据屏幕大小只显示 11 行

ios - 渲染 PDF iOS-iPad-Quartz 时出现问题

ios - 如何使用 cocoapods 为本地库创建一个 pod

ios - 检测滑动删除按钮和在编辑模式下制作的删除按钮之间的区别?

Swift:从 [Int: UIView]() 访问 UIView 子级

ios - 如何在横向模式下使用自动布局设置水平文本字段?