如何将uicollectionview所选项目存储到单个对象中?

时间:2017-08-25 12:31:49

标签: ios swift uicollectionview

背景资料

我有2 class SqliteInterceptor : IDbCommandInterceptor { private static Regex replaceRegex = new Regex(@"\(CHARINDEX\((.*?),\s?(.*?)\)\)\s*?>\s*?0"); public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { } public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { } public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { ReplaceCharIndexFunc(command); } public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { } public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { ReplaceCharIndexFunc(command); } private void ReplaceCharIndexFunc(DbCommand command) { bool isMatch = false; var text = replaceRegex.Replace(command.CommandText, (match) => { if (match.Success) { string paramsKey = match.Groups[1].Value; string paramsColumnName = match.Groups[2].Value; //replaceParams foreach (DbParameter param in command.Parameters) { if (param.ParameterName == paramsKey.Substring(1)) { param.Value = string.Format("%{0}%", param.Value); break; } } isMatch = true; return string.Format("{0} LIKE {1}", paramsColumnName, paramsKey); } else return match.Value; }); if (isMatch) command.CommandText = text; } } 秒。第一个代表印度各州的名单,第二个代表一个大学名单 在第一个UIcollectionView中选择任何州时,相应的大学列表将显示在第二个UICollectionView中。

问题

我的要求是:假设我选择马哈拉施特拉邦的3所学院和卡纳塔克邦的4所学院。我需要参考州名或ID存储选定的大学。

例:
UICollectionView
[Karnataka : collegeA, collegeB, collegeC, collegeD]

代码

[Maharastra : collegeX, collegeY, collegeZ]

2 个答案:

答案 0 :(得分:0)

在包含集合视图的ViewController中,添加类型为[String: [String]]

的类变量

然后在你的didSelectItem中:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView.isEqual(statesCollectionView) {
        state = stateNamesArr[indexPath.item]
        print("Selected state Details : \(stateNamesArr[indexPath.row]) )")
        print("Selected state ID : \(stateIDsArr[indexPath.row]) )")
        selectedStateID = stateIDsArr[indexPath.row]
    } else {
        let selectedCell:collegesCollectionViewCell = collactionView.cellForItem(at: indexPath)! as! ProfessionalTimesCollectionViewCell
        selectedCell.layer.borderColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66).cgColor
        selectedCell.layer.borderWidth = 2
        selectedCell.professionalsTimeLbl.backgroundColor = UIColor.white
        print("selected colleges: \(collegesArr[indexPath.row])")
        selectedCollegesArr.append(collegesArr[indexPath.row])

        //selectedColleges would be the [String: [String]] object -- name it whatever you want

        if selectedColleges[state] != nil {
            selectedColleges[state].append(collegesArr[indexPath.item])
        } else {
            selectedColleges[state] = collegesArr[indexPath.item]
        }

        collegesDict = ["\(selectedStateID)" : [selectedCollegesArr]]
        print("Dict: \(collegesDict)")
    }
}

顺便说一下,由于您使用的是UICollectionView,因此您应该使用indexPath.item访问所选元素 - indexPath.row用于UITableView

答案 1 :(得分:-2)

你可以用Userdefaults

来做
    let defaults = UserDefaults.standard
    // This is set
    defaults.set(yourselectedThings, forKey: "selectedId")

    // Get is
    defaults.object(forKey: "selectedId")