简体   繁体   English

根据用户操作向左或向右移动(使用 Xamarin IOS 和 XIB)在表格单元格视图中排列多个 UIViews

[英]Arrange multiple UIViews with in table cell view based on user action either move to left or right (using Xamarin IOS and XIB)

I'm new to IOS based development.我是基于 IOS 的开发的新手。 I have a following requirement to implement table view.我有以下要求来实现表视图。 That's how the view should arrange based on user action(move to left or right)这就是视图应该如何根据用户操作进行排列(向左或向右移动)

My plan is arranged all the images in Image View and based on the user tap to left/right need to rearrange/hide/show the images.我的计划是在 Image View 中排列所有图像,并根据用户向左/向右点击需要重新排列/隐藏/显示图像。 I'm not able to achieve this dynamically.我无法动态实现这一点。 All help much appreciated.非常感谢所有帮助。

Try the following code , just place the corresponding images into Resource folder.试试下面的代码,把对应的图片放到Resource文件夹中即可。

public class MyCell : UITableViewCell
    {
        private float buttonWidth = 50;
        private float buttonHeight = 100;
        private float largeButtonWidth = 0;
        private List<string> imageList;
        private List<string> largeImageList;
        private List<UIButton> buttonList;

        public MyCell()
        {
            largeButtonWidth = (float)(UIScreen.MainScreen.Bounds.Width - buttonWidth * 2);
            imageList = new List<string> { "1.png", "2.png" , "3.png"};
            largeImageList = new List<string> { "bg1.PNG", "bg2.PNG", "bg3.PNG" };
            buttonList = new List<UIButton>();

            for (int i = 0; i < imageList.Count; i++)
            {
                UIButton button = new UIButton();
                button.Tag = i;
                button.Frame = new CGRect(buttonWidth * i, 0, buttonWidth, buttonHeight);
                button.SetImage(UIImage.FromBundle(imageList[i]), UIControlState.Normal);
                button.TouchUpInside += Button_TouchUpInside;
                this.ContentView.Add(button);
                buttonList.Add(button);
            }
        }

        private void Button_TouchUpInside(object sender, EventArgs e)
        {
            reset();

            UIButton button = sender as UIButton;
            button.SetImage(null, UIControlState.Normal);
            button.SetBackgroundImage(UIImage.FromBundle(largeImageList[(int)button.Tag]), UIControlState.Normal);

            CGRect frame = button.Frame;
            frame.Width = largeButtonWidth;
            button.Frame = frame;

            UIButton button2 = buttonList[1];
            CGRect frame2 = button2.Frame;
            frame2.X = buttonList[0].Frame.Right;
            button2.Frame = frame2;

            UIButton button3 = buttonList[2];
            CGRect frame3 = button3.Frame;
            frame3.X = buttonList[1].Frame.Right;
            button3.Frame = frame3;
        }

        void reset()
        {
            for (int i = 0; i < imageList.Count; i++)
            {
                UIButton button = buttonList[i];

                button.SetImage(UIImage.FromBundle(imageList[i]), UIControlState.Normal);
                button.SetBackgroundImage(null, UIControlState.Normal);

                CGRect frame = button.Frame;
                frame.Width = buttonWidth;
                button.Frame = frame;
            }
        }      
    }

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM