0

我在 UITablViewSource 中使用以下代码

 public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
 {
 UIImage scaledImage=null;
string cellIdentifier = "NewsFeedCell"; 
var newsFeedCellItem = NewsFeedCellItemList [indexPath.Row];    
var newsFeedCell = new NewsFeedCell (NewsFeedScreenInstance, newsFeedCellItem,       cellIdentifier, indexPath);

if (newsFeedCell != null) {
 if (!String.IsNullOrWhiteSpace (newsFeedCellItem.FeedItem.Picture.PreviewUrl)) {
                var image = ImageStore.Get      (newsFeedCellItem.FeedItem.Picture.PreviewUrl);
                if(image != null)
                {
                    newsFeedCellItem.FeedItem.Picture.Image = image;
                    scaledImage = ImageHelper.Scale(image, new SizeF (528, 528));
                }
                if (scaledImage != null) {
                    newsFeedCell.ScrapImage = scaledImage;
                } else {
                    BeginDownloadImage (tableView, indexPath);
                }

            }


        }

        return newsFeedCell;
    }

    #endregion

    #region PRIVATE METHODS

    private void BeginDownloadImage (UITableView tableView, NSIndexPath indexPath)
    {
        Action successAction = () => {

            this.BeginInvokeOnMainThread (() => {
                tableView.BeginUpdates ();
                tableView.ReloadRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
                tableView.EndUpdates ();
            });
        };

        ImageStore.BeginDownloadImage(NewsFeedCellItemList [indexPath.Row].FeedItem.Picture.PreviewUrl, successAction);
    }
    #endregion

*描述: *但是下面的部分代码给出了异常

* -[Scrapboom.iPhone.NewsFeedTableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit/UIKit-2903.2/UITableView.m:1076 并且应用程序被挂起或有时崩溃。

tableView.ReloadRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
4

1 回答 1

2

您的GetCell实现看起来不对。您应该尝试Dequeue一个单元格,然后在它失败时创建一个单元格(在 iOS6+ 中甚至不需要Register*ForCellReuse

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    string cellIdentifier = "NewsFeedCell"; 
    var newsFeedCell = tableView.DequeueReusableCell (cellIdentifier) as NewsFeedCell;

    //only required if you haven't used Register*ForCellReuse
    if (newsFeedCell == null)
        newsFeedCell = new NewsFeedCell (..., cellIdentifier,...);

    //update your cell image and components here.
}

要了解更多信息,请阅读教程

如果您环顾四周,您还会发现经过验证的工作模式可以在表格单元格中延迟加载图像。并不是说您的乍一看看起来不对,但这并不常见。

于 2013-10-17T07:02:52.007 回答