繁体   English   中英

TableViewCell 内的 CollectionView

[英]CollectionView inside TableViewCell

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

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

这可能是因为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;

Swift 5将此添加到您的自定义UITableViewCell类中。

override func prepareForReuse() {

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

        collectionView.dataSource = self
        collectionView.delegate = self
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM