简体   繁体   中英

CollectionView inside TableViewCell

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

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;

Swift 5 Add this in your custom UITableViewCell class.

override func prepareForReuse() {

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

        collectionView.dataSource = self
        collectionView.delegate = self
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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