CollectionView inside TableViewCell

元气小坏坏 提交于 2019-12-24 18:55:40

问题


I used CollectionView inside TableViewCell. All works fine and shown all as expected. But if I scrolled the TableView very fast, items (i used images in collectionView) from one collection replaced with items (images) from another collection and override it on View (on Debug mode in code al works fine, its just displaying of them).

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

回答1:


It's probably because of the reuse system of cells in UITableView. Do you set up properly your data when you configure the cell? Do you call CollectionView's reloadData()?

EDIT: You should call it in the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell where you configure your cell. This way, each time a cell is reused, you update its content.

EDIT 2: Just like I said try to add the collection view reloadData() when you set up your tableview cell. You also have to clean your datasource and delegate, because it's a reused cell so it may already have been used with another value.

 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;


来源:https://stackoverflow.com/questions/59289635/collectionview-inside-tableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!