2016-06-29 48 views
-2

我有一个iOS应用程序,我正在使用嵌套字典将数据提供给表视图。字典的结构是:访问嵌套字典中的单个元素

Dictionary<string1, Dictionary<string2, string3>> dict; 
(I have named the strings here for clarification purposes) 

我目前无法访问此嵌套字典中的单个元素。我想将string1作为单元格标题,string2为单元格文本标签,string3为单元格详细文本标签。这是我目前用表数据源的方法:

public class SupervisionDetailSource:UITableViewSource 
{ 
    Dictionary<string, Dictionary<string, string>> dict; 
    string Identifier = "cell"; 

    public SupervisionDetailSource(Dictionary<string, Dictionary<string, string>> dict) 
    { 
     this.dict = dict; 
    } 

    public override string TitleForHeader(UITableView tableView, nint section) 
    { 
     return dict.Keys.ElementAt((int)section); 
    } 

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     UITableViewCell cell = tableView.DequeueReusableCell(Identifier); 
     if (cell == null) 
      cell = new UITableViewCell(UITableViewCellStyle.Subtitle, Identifier); 

     //this is not right 
     cell.TextLabel.Text = dict[supvList.Keys.ElementAt(indexPath.Section)][dict[dict.Keys.ElementAt(indexPath.Row)]]; //string2 
     cell.DetailTextLabel.Text = string3 ...? 

     return cell; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     string key = dict.Keys.ElementAt((int)section); 
     return dict[key].Count; 

    } 

    public override nint NumberOfSections(UITableView tableView) 
    { 
     return dict.Keys.Count; 
    } 
} 

感谢您的帮助

+0

你所说的 “不对” 是什么意思? – Alexander

+0

@AMomchilov我的意思是我目前的方法来访问我的字典'string2'是不正确的/不可编译 – panthor314

+0

这并不告诉我任何我不知道的东西。这是一个编译器错误?如果是这样,那么错误信息是什么?它是运行时错误吗?如果是这样,什么是堆栈跟踪?这是一个逻辑错误?如果是这样,那么实际产出和预期产出是多少? – Alexander

回答

0

我有困难访问嵌套的元素,但我最终得到它的工作。我只是误解了我需要如何解决这个问题(更不用说在你嵌套越多的时候它就会变得有点毛)。

我的解决方案:

cell.TextLabel.Text = dict[dict.Keys.ElementAt(indexPath.Section)].Keys.ElementAt(indexPath.Row); //string2 
cell.DetailTextLabel.Text = dict[dict.Keys.ElementAt(indexPath.Section)][dict[dict.Keys.ElementAt(indexPath.Section)].Keys.ElementAt(indexPath.Row)]; //string3 
+2

你应该真的尝试清理一下。它非常多毛。 – Alexander