C#iOS滚动问题

时间:2018-07-19 03:19:33

标签: c# ios

我有一个我编写的Xamarin C#IOS应用程序,它使用uitableviewsource显示无序列表。一切看起来都很好,直到我向上滚动,然后文本上升到标题中,而当我向下滚动时,它消失了。

标题的代码如下     公共重写UIView GetViewForHeader(UITableView tableView,nint节)         {             UIView view = new UIView(new Rectangle(0,65,320,90));

        UILabel label = new UILabel
        {
            Opaque = true,
            TextColor = UIColor.Black, //.FromRGB(190, 0, 0);
            Font = UIFont.FromName("Helvetica-Bold", 16f),
            Frame = new RectangleF(12, 70, 375, 20),
            Text = "View Mileage",
            TextAlignment = UITextAlignment.Center
        };
        view.AddSubview(label);

        UIButton buttonRect = new UIButton
        {
            Opaque = true,
            BackgroundColor = UIColor.FromRGB(191, 187, 189),
            Font = UIFont.FromName("Helvetica-Bold", 16f),
            Frame = new CGRect(145, 19, 100, 40)
        };
        buttonRect.SetTitle("Main Menu", UIControlState.Normal);
        buttonRect.SetTitleColor(UIColor.Black, UIControlState.Normal);
        buttonRect.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
        buttonRect.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        buttonRect.TouchUpInside += delegate
        {
            UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);

            var webController = Storyboard.InstantiateViewController("MainMenuViewController") as MainMenuViewController;

            Window = new UIWindow(UIScreen.MainScreen.Bounds);
            this.Window.RootViewController = webController;
            this.Window.MakeKeyAndVisible();

        };
        view.AddSubview(buttonRect);

        UIButton buttonDate = new UIButton
        {
            Opaque = true,
            BackgroundColor = UIColor.Brown,
            Font = UIFont.FromName("Helvetica-Bold", 13f),
            Frame = new CGRect(35, 105, 50, 25)
        };
        buttonDate.SetTitle("Date", UIControlState.Normal);
        buttonDate.SetTitleColor(UIColor.White, UIControlState.Normal);
        buttonDate.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
        buttonDate.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        buttonDate.TouchUpInside += delegate
        {
            dataList.Sort((x, y) => x.Date.CompareTo(y.Date));
            if (dateCounter % 2 != 0)
                dataList.Reverse();
            tableView.ReloadData();
            dateCounter++;
        };
        view.AddSubview(buttonDate);

        UIButton buttonMiles = new UIButton
        {
            Opaque = true,
            BackgroundColor = UIColor.Brown,
            Font = UIFont.FromName("Helvetica-Bold", 13f),
            Frame = new CGRect(100, 105, 50, 25)
        };
        buttonMiles.SetTitle("Miles", UIControlState.Normal);
        buttonMiles.SetTitleColor(UIColor.White, UIControlState.Normal);
        buttonMiles.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
        buttonMiles.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        buttonMiles.TouchUpInside += delegate
        {
            dataList.Sort((x, y) => x.Miles.CompareTo(y.Miles));
            if (milesCounter % 2 == 0)
                dataList.Reverse();
            tableView.ReloadData();
            milesCounter++;
        };
        view.AddSubview(buttonMiles);

        UIButton buttonGas = new UIButton
        {
            Opaque = true,
            BackgroundColor = UIColor.Brown,
            Font = UIFont.FromName("Helvetica-Bold", 13f),
            Frame = new CGRect(174, 105, 50, 25)
        };
        buttonGas.SetTitle("Gas", UIControlState.Normal);
        buttonGas.SetTitleColor(UIColor.White, UIControlState.Normal);
        buttonGas.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
        buttonGas.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        buttonGas.TouchUpInside += delegate
        {
            dataList.Sort((x, y) => x.Gas.CompareTo(y.Gas));
            if (gasCounter % 2 == 0)
                dataList.Reverse();
            tableView.ReloadData();
            gasCounter++;
        };
        view.AddSubview(buttonGas);

        UIButton buttonMpg = new UIButton
        {
            Opaque = true,
            BackgroundColor = UIColor.Brown,
            Font = UIFont.FromName("Helvetica-Bold", 13f),
            Frame = new CGRect(244, 105, 50, 25)
        };
        buttonMpg.SetTitle("MPG", UIControlState.Normal);
        buttonMpg.SetTitleColor(UIColor.White, UIControlState.Normal);
        buttonMpg.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
        buttonMpg.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        buttonMpg.TouchUpInside += delegate
        {
            dataList.Sort((x, y) => x.MPG.CompareTo(y.MPG));
            if (mpgCounter % 2 == 0)
                dataList.Reverse();
            tableView.ReloadData();
            mpgCounter++;
        };
        view.AddSubview(buttonMpg);

        UIButton buttonCost = new UIButton
        {
            Opaque = true,
            BackgroundColor = UIColor.Brown,
            Font = UIFont.FromName("Helvetica-Bold", 13f),
            Frame = new CGRect(314, 105, 50, 25)
        };
        buttonCost.SetTitle("Cost", UIControlState.Normal);
        buttonCost.SetTitleColor(UIColor.White, UIControlState.Normal);
        buttonCost.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
        buttonCost.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        buttonCost.TouchUpInside += delegate
        {
            dataList.Sort((x, y) => x.Price.CompareTo(y.Price));
            if (costCounter % 2 == 0)
                dataList.Reverse();
            tableView.ReloadData();
            costCounter++;
        };
        view.AddSubview(buttonCost);
        return view;
    }

我正在使用UITableViewCell如下填充表格视图;

public void SetData(string str0, string str1, string str2, string str3, int id, string str4)
        {
            lbC0.Text = str0;
            lbC1.Text = str1;
            lbC2.Text = str2;
            lbC3.Text = str3;
            lbcst.Text = str4;
            lbId.Text = id.ToString();
        }

        public override void LayoutSubviews()
        {
            nfloat lbWidth = Bounds.Width / 4;
            nfloat lbWidth2 = lbWidth - 35;
            nfloat lbWidth3 = lbWidth2 + 70;
            nfloat lbWidth4 = lbWidth3 + 70;
            nfloat lbWidth5 = lbWidth4 + 65;
            nfloat lbWidth6 = lbWidth5 + 75;
            nfloat lbHeight = this.Bounds.Height;
            if (lbC0 != null)
            {
                lbC0.Frame = new CGRect(2, 55, lbWidth, lbHeight);
                lbC1.Frame = new CGRect(lbWidth2, 55, lbWidth, lbHeight);
                lbC2.Frame = new CGRect(lbWidth3, 55, lbWidth, lbHeight);
                lbC3.Frame = new CGRect(lbWidth4, 55, lbWidth, lbHeight);
                lbcst.Frame = new CGRect(lbWidth5, 55, lbWidth, lbHeight);
            }
        }

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

我花了数周时间寻找答案。最终发现,我需要在TableViewCell的范围内放置子视图的范围。一切都很好,但是我不知道该怎么做。我尝试了所有发现的尝试,但都无济于事。最终为我解决了这个问题。在我的LayoutSubviews方法中,设置子视图的高度时,我从Bounds.Height中减去了100并对其进行了固定,现在它可以上下滚动。修复有多简单...很难找到

这是我更改的代码;

public override void LayoutSubviews()
        {
            nfloat lbWidth = Bounds.Width / 4;
            nfloat lbWidth2 = lbWidth - 35;
            nfloat lbWidth3 = lbWidth2 + 70;
            nfloat lbWidth4 = lbWidth3 + 70;
            nfloat lbWidth5 = lbWidth4 + 65;
            nfloat lbWidth6 = lbWidth5 + 75;
         -->nfloat lbHeight = this.Bounds.Height - 100;<--
            if (lbC0 != null)
            {
                lbC0.Frame = new CGRect(2, 55, lbWidth, lbHeight);
                lbC1.Frame = new CGRect(lbWidth2, 55, lbWidth, lbHeight);
                lbC2.Frame = new CGRect(lbWidth3, 55, lbWidth, lbHeight);
                lbC3.Frame = new CGRect(lbWidth4, 55, lbWidth, lbHeight);
                lbcst.Frame = new CGRect(lbWidth5, 55, lbWidth, lbHeight);
            }
        }