我想要为从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 ();
答案 0 :(得分: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.