如何使用Xamarin iOS和来自API(JSON)的文本以编程方式实时制作按钮?

时间:2018-08-09 19:03:47

标签: ios swift visual-studio xamarin.ios mvvmcross

我想即时添加按钮。不知道每个“收藏夹视图”单元有多少个,例如,单个单元可以是一个按钮“标签”,下一个单元可以是3个按钮,或者我的所有单元总计为10个,则可以为零。一切取决于我的Api,即每个单元格有多少个“标签”按钮。简而言之->我从json获取标签->然后动态创建按钮--->然后将按钮文本与json api中的字符串绑定。我正在使用Mvvmcross iOS this is a demo image

DEMO Json  --> {
      "tags": [],
      "custom_tags": [
        "Football",
        "Latest news",
        "News",
        "abc",
        "abc football",
        "abc Football",
        "abc Ladies Football",
        "abc Women's Football",
        "abc football",
        "123 Football",
        "111Football Ladies",
        "womens",
        "Womens Football"
      ]}

Mvvmcross iOS演示->

UIButton button = new UIButton();
button.Frame = new RectangleF(0, 60, 50, 30);
button.SetTitle("Title", UIControlState.Normal);
button.BackgroundColor = UIColor.Black;

UIButton button2 = new UIButton();
button2.Frame = new RectangleF(60, 60, 50, 30);
button2.SetTitle("Title 2", UIControlState.Normal);
button2.BackgroundColor = UIColor.Black;


this.AddSubview(button2);
this.AddSubview(button);

1 个答案:

答案 0 :(得分:0)

只需遍历标签列表并动态创建按钮

foreach (var tag in custom_tags) {
  UIButton button = new UIButton();
  button.SetTitle(tag, UIControlState.Normal);
  // do other config here
  this.AddSubview(button)
}