簡體   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