0

我正在创建一个基于 Monotouch 元素的元素。这样做的结果是创建一个列表(如 Twitter 时间线),每个单元格中都有主题、域名和 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;
    }
4

1 回答 1

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部分...

我会说你犯了一个错误,就是把一种方法写得太长,以至于你都看不懂。

考虑使用诸如“提取方法”之类的重构工具,以便将其分解为更小的有意义的函数。看

这都是正常重构的一部分——在一个函数中编写代码,然后将其重构为更小的部分,因为它变得太长且笨重......

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

...所以无论您做什么,都会找到适合您和您的代码的东西:)

于 2013-02-11T18:19:47.503 回答