C# 在列表中呈现在彼此顶部的图像

C# 在列表中呈现在彼此顶部的图像,c#,xamarin.ios,C#,Xamarin.ios,我正在创建一个基于Monotouch元素的元素。这样做的结果是创建一个列表(如Twitter时间线),每个单元格中都有一个主题、域名和Fav图标。问题是: 列表的第一个和最后一个fav图标显示两次,因此它们位于彼此的顶部 下面是代码。有人能看出我犯了什么错误吗/罗伯特 public override void Draw (RectangleF rect) { const int padright = 12;//21; var ctx = UIGraphics.GetCurrent

我正在创建一个基于Monotouch元素的元素。这样做的结果是创建一个列表(如Twitter时间线),每个单元格中都有一个主题、域名和Fav图标。问题是: 列表的第一个和最后一个fav图标显示两次,因此它们位于彼此的顶部

下面是代码。有人能看出我犯了什么错误吗/罗伯特

public override void Draw (RectangleF rect)
{
    const int padright = 12;//21;
    var ctx = UIGraphics.GetCurrentContext ();
    bool highlighted = (Superview.Superview as UITableViewCell).Highlighted;

                var bounds = Bounds;
                var midx = bounds.Width/2;
                if (highlighted){
                    UIColor.FromRGB (4, 0x79, 0xef).SetColor ();
                    ctx.FillRect (bounds);
                    //Images.MenuShadow.Draw (bounds, CGBlendMode.Normal, 0.5f);
                    //textColor = UIColor.White;
                } else {
                    UIColor.White.SetColor ();
                    ctx.FillRect (bounds);
                    ctx.DrawLinearGradient (bottomGradient, new PointF (midx, bounds.Height-17), new PointF (midx, bounds.Height), 0);
                    ctx.DrawLinearGradient (topGradient, new PointF (midx, 1), new PointF (midx, 3), 0);
                }

        float boxWidth;
        float boxHeight = Bounds.Height;
        boxWidth = 0;
        //SizeF ssize;
        //string label;
        //label = Date;
        //ssize = StringSize (label, SubjectFont);
        //float dateSize = ssize.Width + padright + 5;
        //DrawString (label, new RectangleF (Bounds.Width-dateSize-5, 5, dateSize, 14), DateFont, UILineBreakMode.Clip, UITextAlignment.Right);

        const int offset = 46; //33;
        float bw = Bounds.Width-offset;
        UIColor.Black.SetColor ();

        //Extracting the domain name that I get.
        string domainName = new Uri(Sender).DnsSafeHost;
        if(domainName.StartsWith("www"))
        {
            domainName = domainName.Substring(4, domainName.Length-4);
        }

        //Checking to see if there is any subdomain.
        //If so, I start at the first dot to get the domainname "clean".
        int countDots = domainName.Split('.').Length - 1;
        if(countDots > 1)
        {
        Console.WriteLine("Dots: " + countDots);
        int i = domainName.IndexOf('.') + 1;
        Console.WriteLine("Position: " + i);
            domainName = domainName.Substring(i).Trim();
            Console.WriteLine("Domain: " + domainName);
        }

        DrawString (Subject, new RectangleF (offset, 6, bw-boxWidth-5, 24), SubjectFont, UILineBreakMode.TailTruncation, UITextAlignment.Left);
        DrawString (domainName, new PointF (offset, 28), bw/*-dateSize*/, SenderFont, UILineBreakMode.TailTruncation);

        //Getting the fav icon from the domainname and posting it to g.etfv.co.
        string favUrl = "http://www."+domainName;
        Console.WriteLine("Fav icon: " + favUrl);
        Uri imageBackground = null;
        imageBackground = new Uri ("http://g.etfv.co/"+ favUrl);
        var avatar = ImageLoader.DefaultRequestImage (imageBackground, this);
        var imageRect = new RectangleF(15f, 10f, 16f, 16f); 
        using (var myImage = new UIImageView(imageRect))
        {
            if(avatar == null)
            {
                //A local avatar if something goes wrong
                string localAvatar = "Images/avatar.png";
                myImage.Image = UIImage.FromFile(localAvatar);
            }
            else
            {myImage.Image = avatar;}
            AddSubview(myImage);
        }
        avatar = null;
    }
有人能看出我犯了什么错误吗

第一部分。。。 你的描述“因此它们在彼此之上”并不完全清楚

但是我猜这个问题可能隐藏在这个方法的背后

        var avatar = ImageLoader.DefaultRequestImage (imageBackground, this);
如果这是异步请求化身,那么您需要确保当异步方法返回时,该单元没有被重新用于其他位置

在TweetStation中,这是在
void IImageUpdated.UpdateImage(long onId)
回调中的以下行中完成的:

    // Discard notifications that might have been queued for an old cell
    if (tweet == null || (tweet.UserId != onId && tweet.RetweeterId != onId))
        return;
您的回拨中是否有类似的行


有人能看出我犯了什么错误吗

第二部分。。。 我想说,你犯了一个错误,把一种方法做得太久了,连你自己都看不懂

考虑使用类似“提取方法”的方法,以便将其分解为更小的有意义的函数。看

这都是正常重构的一部分-在一个函数中编写代码,然后当它变得太长和笨拙时将其重构为更小的部分

。。。很多人会说我写的方法太小了


。。。因此,无论您做什么,只要找到适合您和您的代码的东西:)

嘿,我有以下代码:public void updateImage(Uri imageBackground){ImageLoader.DefaultRequestImage(imageBackground,this);}