根据用户操作向左或向右移动,在 table 单元格视图中排列多个 UIView(使用 Xamarin IOS 和 XIB)
Arrange multiple UIViews with in table cell view based on user action either move to left or right (using Xamarin IOS and XIB)
我是 IOS 开发的新手。我有以下要求来实现 table 视图。That's how the view should arrange based on user action(move to left or right)
我的计划是在图像视图中排列所有图像,并根据用户点击 left/right 需要 rearrange/hide/show 图像。我无法动态地实现这一点。非常感谢大家的帮助。
试试下面的代码,只需将相应的图像放入 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;
}
}
}
我是 IOS 开发的新手。我有以下要求来实现 table 视图。That's how the view should arrange based on user action(move to left or right)
我的计划是在图像视图中排列所有图像,并根据用户点击 left/right 需要 rearrange/hide/show 图像。我无法动态地实现这一点。非常感谢大家的帮助。
试试下面的代码,只需将相应的图像放入 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;
}
}
}