我有一个UICollectionView,其中包含有图像的单元格。这些图像是从网址下载的。问题是每当我滚动UICollectionView时,都会重新加载单元格,并且单元格中会出现重复的图像几秒钟。
这完全扰乱了细胞。以下是我的一些代码
var pictureCollectionSource = new PictureCollectionSource(picRefined);
pictureCollection.Source = pictureCollectionSource;
picturecollectiondelegate = new PictureCollectionViewDelegate(picRefined);
pictureCollection.Delegate = picturecollectiondelegate;
PictureCollectionViewDelegate.cs
public class PictureCollectionViewDelegate : UICollectionViewDelegateFlowLayout
{
public delegate void CellClickedEvent(object sender, EventArgs e);
public event CellClickedEvent CellClicked;
private IEnumerable<ProfileValues> picRefined;
public PictureCollectionViewDelegate(IEnumerable<ProfileValues> picRefined)
{
// TODO: Complete member initialization
this.picRefined = picRefined;
}
public override CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath)
{
var width = (UIScreen.MainScreen.Bounds.Size.Width / 4);
return new CoreGraphics.CGSize(width, width);
}
}
PictureCollectionSource.cs
public override UICollectionViewCell GetCell(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
var cell = collectionView.DequeueReusableCell (cell_id, indexPath) as PictureCollectionCell ?? PictureCollectionCell.Create();
var data = picRefined.ElementAt(indexPath.Row);
cell.UpdateCell(data);
return cell;
}
UpdateCell
internal void UpdateCell(ProfileValues mdata)
{
var mUrl = "someurl";
var manager = SDWebImageManager.SharedManager;
manager.ImageCache.MaxCacheAge = 86400;
SDWebImageDownloader mDownloader = manager.ImageDownloader;
var authToken = "SomeToken"";
mDownloader.SetHttpHeaderValue(authToken, "Authorization");
mDownloader.SetHttpHeaderValue("application/json; odata=verbose", "Accept");
mDownloader.SetHttpHeaderValue("f", "X-FORMS_BASED_AUTH_ACCEPTED");
try
{
mDownloader.DownloadImage(
url: new NSUrl(mUrl),
options: SDWebImageDownloaderOptions.ProgressiveDownload,
progressBlock: (receivedSize, expectedSize) =>
{
// Track progress...
},
completedBlock: (image, data, error, finished) =>
{
if (image != null && finished)
{
InvokeOnMainThread(() =>
{
this.imgProfilePic.Image = image;
});
}
if (error != null)
{
InvokeOnMainThread(() =>
{
this.imgProfilePic.Image = errorImage;
this.imgProfilePic.BackgroundColor = UIColor.FromRGB(52, 31, 71);
//this.imgProfilePic.Layer.CornerRadius = this.imgProfilePic.Frame.Size.Width / 2;
this.imgProfilePic.ClipsToBounds = true;
this.imgProfilePic.Layer.BorderWidth = 3.0f;
this.imgProfilePic.Layer.BorderColor = UIColor.White.CGColor;
});
}
}
);
}
catch (Exception ex)
{
}
}
修改
我尝试使用评论中建议的静态单元格。我在故事板UICollectionView中添加了16个单元格。修改了我的UICollectionVIewSource,如下所示: -
List<string> cellIds = new List<string>(){"pic_collection_cell","cell_2","cell_3","cell_4","cell_5","cell_6"
,"cell_7","cell_8","cell_9","cell_10","cell_11","cell_12","cell_13","cell_14","cell_15","cell_16"};
public override UICollectionViewCell GetCell (UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
int cellIndex = (int)indexPath.Item;
if (cellIndex >= cellIds.Count) {
cellIndex = 0;
}
UICollectionViewCell cell = collectionView.DequeueReusableCell (cellIds [cellIndex], indexPath) as UICollectionViewCell;
profImage = new UIImageView (cell.Frame);
profImage.Image = UIImage.FromFile ("ic_account.png");
cell.AddSubview (profImage);
return cell;
}
但有了这个,即使调试器显示将imageview添加到其他单元格,只有第一个单元格似乎显示了imageview。
我该如何解决这个问题?任何帮助表示赞赏。谢谢。