[英]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
中單元格的重用系統。 您在配置單元時是否正確設置了數據? 你調用CollectionView
的reloadData()
嗎?
編輯:您應該在配置單元格的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.