TableViewCell 内的 CollectionView

Posted

技术标签:

【中文标题】TableViewCell 内的 CollectionView【英文标题】:CollectionView inside TableViewCell 【发布时间】:2019-12-11 15:57:54 【问题描述】:

我在TableViewCell 中使用了CollectionView。一切正常,并按预期显示。但是,如果我非常快地滚动TableView,则一个集合中的项目(我在collectionView中使用图像)替换为另一个集合中的项目(图像)并在视图上覆盖它(在代码中的调试模式下工作正常,它只是显示他们)。

UITableView GetCell():

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    
        var item = _view.Items[indexPath.Row];
        var cell = (MyTableCell)tableView.DequeueReusableCell(“cell”);
        cell.TextLabelView.Text = item.Title;
        cell.YesButtonView.Hidden = item.IsCategory;
        cell.NoButtonView.Hidden = item.IsCategory;
        if (item.IsImagePoint)
        
            cell.ImagesCollectionView.DataSource = new ItemsDataSource(item.Images, cell.ImagesCollectionView);
            cell.ImagesCollectionView.Delegate = new ItemsDelegate(item, _view);
        
        return cell;
    

UICollectionView GetCell():

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    
            var cell = (ImageViewCell)_collectionView.DequeueReusableCell(new NSString(“ImageViewCell”), indexPath);
            var image = _images[indexPath.Row];
            var imagePath = image.ThumbnailPath;
            if (!string.IsNullOrEmpty(imagePath))
            
                cell.ImagePath = imagePath;
            
            return cell;
    

【问题讨论】:

您能否也为cellForItemAtIndexPath 添加您的代码? (集合视图和表格视图) 与我们分享您的代码,然后我们可以为您提供更好的帮助。这可能是由于重复使用单元格造成的。 是的,你是对的。向问题添加代码 【参考方案1】:

这可能是因为UITableView 中单元格的重用系统。配置单元时是否正确设置了数据?你打电话给CollectionViewreloadData()吗?

编辑:您应该在配置单元格的tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 中调用它。这样,每次重复使用单元格时,您都会更新其内容。

编辑 2:就像我说的那样,在设置 tableview 单元格时尝试添加集合视图 reloadData()。您还必须清理数据源和委托,因为它是一个重复使用的单元格,因此它可能已经与另一个值一起使用。

 if (item.IsImagePoint)
    
        cell.ImagesCollectionView.DataSource = new ItemsDataSource(item.Images, cell.ImagesCollectionView);
        cell.ImagesCollectionView.Delegate = new ItemsDelegate(item, _view);
    
 else
    
        cell.ImagesCollectionView.DataSource = null;
        cell.ImagesCollectionView.Delegate = null;
    

 cell.ImagesCollectionView.ReloadData()

 return cell;

【讨论】:

刚刚编辑,但我不知道 Xamarin,所以你应该适应。但想法是,当您重用一个单元格时,您必须每次都配置它(当您的item.IsImagePointfalse 时也是如此)并且您必须告诉集合视图重新加载。 非常感谢!你救了我。惊人的!我的啤酒;)我已经根据 Xamarin 语法编辑了你的答案(小改动)。【参考方案2】:

斯威夫特 5 将此添加到您的自定义 UITableViewCell 类中。

override func prepareForReuse() 

        collectionView.dataSource = nil
        collectionView.delegate = nil
        collectionView.reloadData()

        collectionView.dataSource = self
        collectionView.delegate = self
  

【讨论】:

我的案例 tableview 单元格嵌入了带有子类布局的 collectionview,这段代码就像魅力一样工作。

以上是关于TableViewCell 内的 CollectionView的主要内容,如果未能解决你的问题,请参考以下文章

未调用 TableViewCell 内的 UICollectionView

更改 TableViewCell 内的 UIPageControl 的 CurrentPage 时出现问题

TableViewCell 内的 UItextField 包含多个部分 - 数据重复

无法单击 iOS 14 中 TableViewCell 内的按钮 [重复]

原型 tableviewcell 内的 UIPickerView 没有选择指示器

tableviewcell 内的 UIButton 触发单元格而不是按钮 [重复]