Forum Cross Platform with Xamarin

Iterate over a list to create SwitchCells for each string in the list

(i have accidentally posted this question on a discussion, sorry!)

HI Im relatively new to Xamarin and c# :
so i have a list of strings
var tags = RoblyApi.LoadFields();
what i would like to do is, iterate over this list to form a SwitchCell on a TableView or ListView that will display each string on the list
What im trying to accomplish is: giving a user the ability to select which of the items on this list he would like on his forum.
TableView tableView = new TableView

this is the example Xamarin had for TableView. How would i loop through to form a new SwitchCell for each tag
Every time i did try to add an foreach loop I get many syntactical errors.

{
    Intent = TableIntent.Form,
 Root = new TableRoot
 {
        new TableSection
      {
      new SwitchCell
       {
          Text = "hello"
       }
    }
}

};
 this.Content = new StackLayout
{
    Children =
      {
    header,
    tableView
}
};

Thank You for your help :)

Best Answer

Answers

  • ShehryarKhanShehryarKhan USMember

    I SOLVED IT!

    HERE IS THE CODE :smile:

    {

            var tags = Api.LoadFields();
            var section = new TableSection("labels");
            var table = new TableView();
            table.Intent = TableIntent.Settings;
            foreach (var o in tags)
            {
                var cell = new SwitchCell { Text = o };
                section.Add(cell);
    
            }
    
            TableView tableview = new TableView
            {
                Root = new TableRoot
                {
                }
            };
    
            tableview.Root.Add(section);
    
    
    
    
            this.Content = new StackLayout
            {
                Children =
                {
                    tableview
                }
            };
    
        }
    
  • ShehryarKhanShehryarKhan USMember

    I SOLVED IT!

    HERE IS THE CODE :smile:

    {

            var tags = Api.LoadFields();
            var section = new TableSection("labels");
            var table = new TableView();
            table.Intent = TableIntent.Settings;
            foreach (var o in tags)
            {
                var cell = new SwitchCell { Text = o };
                section.Add(cell);
    
            }
    
            TableView tableview = new TableView
            {
                Root = new TableRoot
                {
                }
            };
    
            tableview.Root.Add(section);
    
    
    
    
            this.Content = new StackLayout
            {
                Children =
                {
                    tableview
                }
            };
    
        }
    
Sign In or Register to comment.