在列表中呈现在彼此之上的图像

时间:2013-02-11 17:46:48

标签: c# xamarin.ios

我正在创建一个基于Monotouch元素的元素。这会发生的是创建一个列表(如Twitter时间轴),每个单元格中包含主题,域名和Fav-Icon。问题是这样的: 列表的第一个和最后一个fav-icon显示两次,因此它们相互叠加。

以下是代码。任何人都可以看到我犯了什么错误? /罗伯特

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;
    }

1 个答案:

答案 0 :(得分:0)

  

有谁能看出我犯了什么错误?

第1部分......

你的描述'因此它们彼此重叠'并不完全清楚。

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

        var avatar = ImageLoader.DefaultRequestImage (imageBackground, this);

如果这是异步请求头像,那么你需要确保当异步方法返回时,该单元格还没有被重用于其他位置。

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

    // Discard notifications that might have been queued for an old cell
    if (tweet == null || (tweet.UserId != onId && tweet.RetweeterId != onId))
        return;

你的回调中是否有类似的行?


  

有谁能看出我犯了什么错误?

第2部分......

我会说你错误地制作了一个方法,即使你不能阅读它。

考虑使用refactoring tools之类的“提取方法”,以便将其分解为更有意义的小功能。见

这是正常重构的一部分 - 在一个函数中编写代码然后将其重构为较小的部分,因为它变得太长而且难以处理......

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

...所以无论你做什么,只要找到适合你和你的代码的东西:)