创建表格选择视图的最有效方式,标题为swift& IOS

时间:2017-04-21 11:19:45

标签: ios arrays swift uitableview dictionary

我想要一个用于选择键值对的表视图。该表可以重复使用,但是这里有一个使用班级和学生的例子:

11A/It
4125:John
5121:Ben
6121:Anna
4125:Gemma
10B/It
5121:Ben
6121:Anna
4125:Gemma
4512:Kevin

班级名称是标题 - 它们不可选择。学生是可选择的,表格视图将通过协议传回密钥和值。

重要的是,这些都保持有序,这意味着我的第一次尝试不起作用:

var data = [String:[String:String]()
// Causes problems, because dictionaries have no order

所以,我以为我可以将词典变成数组:

var data = [[String:[[String:String]]]()

这允许我获取部分的数量,但后来我无法弄清楚如何获得行数:

func numberOfSections(in tableView: UITableView) -> Int {
    return data.count
}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data[section].first.count // Nope!
}

所以,我认为我正在寻找关于最佳方法的一些一般性建议。我可以通过两种方式看到这个:

  1. 分别有一系列标题和数据,所以我可以做一些像`data [headings [section]] [row]
  2. 创建自定义键:值类型并使用
  3. 我有一种感觉,虽然我可能会过度复杂化 - 是否有一种标准的可接受方式来解决这个问题。这似乎是一个非常普遍的事情?非常感谢提前!

1 个答案:

答案 0 :(得分:1)

我会创建一个可以保存数据的结构,而不仅仅是一个抽象字典。

例如

editText.Touch += (sender, e) => {
                    e.Handled = false;
                    if (e.Event.Action == MotionEventActions.Up)
                    {
                        if (e.Event.RawX >= (bibEditText.Right - (bibEditText.GetCompoundDrawables()[2]).Bounds.Width()))
                        {
                            SearchRunner();
                            InputMethodManager manager = (InputMethodManager)GetSystemService(InputMethodService);
                            manager.HideSoftInputFromWindow(editText.WindowToken, 0);
                            e.Handled = true;
                        }
                    }
                };