0

我想对从 JSON 响应中返回的单元格进行样式设置。我可以遍历元素,但是一旦我尝试设置它们的样式,就会出现以下错误。

我错过了什么?

! - 错误

收藏已修改;枚举操作可能无法执行。

! - 代码

string responseString = string.Empty;
        Uri uri = new Uri ("http://myhost/sample.json");
        HttpWebRequest request = new HttpWebRequest (uri);
        request.Method = "GET";

        HttpWebResponse response = request.GetResponse () as HttpWebResponse;
        var obj = JsonValue.Load (new StreamReader (response.GetResponseStream())) as JsonObject;
        if (obj != null) {


            var root = JsonElement.FromJson (obj);
            var jsonSection = root["section-1"] as Section;

            UILabel headerLabel = new UILabel();

            headerLabel.BackgroundColor = UIColor.White;
            headerLabel.Opaque = false;
            headerLabel.TextColor = UIColor.Blue;
            headerLabel.HighlightedTextColor = UIColor.White;
            headerLabel.Font = UIFont.BoldSystemFontOfSize (22);
            headerLabel.Frame = new RectangleF(8,0,200,60);
            headerLabel.Text = jsonSection.Caption;

            jsonSection.HeaderView = headerLabel;

            Console.WriteLine("before");


            foreach (var elmList in jsonSection)
            {
                Console.WriteLine("test");



                var element = new StyledStringElement(elmList.ToString())
                {

                    TextColor = UIColor.Red,
                    BackgroundColor = UIColor.Brown,
                    Accessory = UITableViewCellAccessory.DisclosureIndicator
                };

                jsonSection.Elements.Add(element);


            }



            var dvc = new DialogViewController (root, true);
            navigation.PushViewController (dvc, true);
        }



        response.Close ();
4

1 回答 1

0

您不能修改 foreach 枚举。改用 for 循环并调整索引

您收到错误是因为

 jsonSection.Elements.Add(element);

我不知道这段代码(在我脑海中)你在做什么应该有效:

 var tmpSection = new Section();
 foreach(var elmList in jsonSection)
        {
            Console.WriteLine("test");

            var element = new StyledStringElement(elmList.ToString())
            {

                TextColor = UIColor.Red,
                BackgroundColor = UIColor.Brown,
                Accessory = UITableViewCellAccessory.DisclosureIndicator
            };

            tmpSection.Elements.Add(element);


        }

        jsonSection.Elements.AddRange(tmpSection); //it this doesn't work, loop through tmpSection and add the elements to jsonSection.
于 2013-04-12T22:03:51.457 回答