【发布时间】:2018-04-05 09:07:36
【问题描述】:
我无法使用类型转换 Array<Dictionary<String,Any>> 检索计数信息。现在我正在使用 Swift 4.1 开始新项目。
我在 UITableView(分组)上显示了带有节标题和行的静态数据。
我随后通过此链接显示GDSM 发布的数据 - How to form static data with the structure like array of objects in swift 4? (It's also I have asked before forming this kind of structures using NSMutableArray like in Objective c)
在 ViewDidload 中我分配了 var listOfScoresInfoArray = Array<Dictionary<String,Any>> ()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.clear
listOfScoresInfoArray = prepareMatchInfo()
print(listOfScoresInfoArray)
}
func prepareMatchInfo() -> Array<Dictionary<String,Any>> {
var arrayOfMatches : Array<Dictionary<String,Any>> = Array<Dictionary<String,Any>>();
var arrayOfScoresSectionOne : Array<Dictionary<String,Any>> = Array<Dictionary<String,Any>>();
var arrayOfScoresSectionTwo : Array<Dictionary<String,Any>> = Array<Dictionary<String,Any>>();
arrayOfScoresSectionOne.append(getTeamScoreDetails(lsTeamName: "Russia", rsTeamName: "S.Arabia", lsPredictionPtsForWinOrDraw: "W : 16/X : 20", rsPredictionPtsForWinOrDraw: "W : 25/X : 20", finalScore: "2-1", predictionScore: "2-1", points: 16, startTime: "", matchStatus: WCPConstants.matchStatus.matchStatusEnd, predictionResults: WCPConstants.predictionResults.predictionResultsWin));
arrayOfScoresSectionTwo.append(getTeamScoreDetails(lsTeamName: "Egypt", rsTeamName: "Uruguay", lsPredictionPtsForWinOrDraw: "W : 30/X : 25", rsPredictionPtsForWinOrDraw: "W : 16/X : 25", finalScore: "1-1", predictionScore: "1-3", points: 0, startTime: "18:00 - TF1", matchStatus: WCPConstants.matchStatus.matchStatusLive, predictionResults: WCPConstants.predictionResults.predictionResultsNone));
arrayOfScoresSectionTwo.append(getTeamScoreDetails(lsTeamName: "Marocco", rsTeamName: "Iran", lsPredictionPtsForWinOrDraw: "W : 20/X : 20", rsPredictionPtsForWinOrDraw: "W : 22/X : 20", finalScore: "1-1", predictionScore: "2-2", points: 16, startTime: "", matchStatus: WCPConstants.matchStatus.matchStatusEnd, predictionResults: WCPConstants.predictionResults.predictionResultsDraw));
arrayOfScoresSectionTwo.append(getTeamScoreDetails(lsTeamName: "Portugal", rsTeamName: "Spain", lsPredictionPtsForWinOrDraw: "W : 30/X : 12", rsPredictionPtsForWinOrDraw: "W : 14/X : 12", finalScore: "1-1", predictionScore: "2-2", points: 10, startTime: "", matchStatus: WCPConstants.matchStatus.matchStatusEnd, predictionResults: WCPConstants.predictionResults.predictionResultsDraw));
arrayOfScoresSectionTwo.append(getTeamScoreDetails(lsTeamName: "France", rsTeamName: "Australia", lsPredictionPtsForWinOrDraw: "W : 10/X : 28", rsPredictionPtsForWinOrDraw: "W : 34/X : 28", finalScore: "1-1", predictionScore: "1-3", points: 0, startTime: "", matchStatus: WCPConstants.matchStatus.matchStatusEnd, predictionResults: WCPConstants.predictionResults.predictionResultsLost));
arrayOfMatches.append(getMatchInfo(date: "2017-06-14", data: arrayOfScoresSectionOne))
arrayOfMatches.append(getMatchInfo(date: "2017-06-15", data: arrayOfScoresSectionTwo))
return arrayOfMatches;
}
func getMatchInfo(date:String, data:Array<Dictionary<String, Any>>) -> Dictionary<String, Any> {
var dict : Dictionary<String, Any> = Dictionary();
dict["Data"] = data;
dict["Date"] = date;
return dict;
}
func getTeamScoreDetails(lsTeamName:String, rsTeamName:String, lsPredictionPtsForWinOrDraw:String, rsPredictionPtsForWinOrDraw:String, finalScore:String, predictionScore:String, points:CGFloat, startTime:String, matchStatus:WCPConstants.matchStatus, predictionResults:WCPConstants.predictionResults) -> Dictionary<String, Any> {
var dict : Dictionary<String, Any> = Dictionary();
dict["lsTeamName"] = lsTeamName;
dict["rsTeamName"] = rsTeamName;
dict["lsPredictionPtsForWinOrDraw"] = lsPredictionPtsForWinOrDraw;
dict["rsPredictionPtsForWinOrDraw"] = rsPredictionPtsForWinOrDraw;
dict["finalScore"] = finalScore;
dict["predictionScore"] = predictionScore;
dict["points"] = points;
dict["startTime"] = startTime;
dict["matchStatus"] = matchStatus;
dict["predictionResults"] = predictionResults;
return dict;
}
func getNumberOfRowsCountForSections(section: Int) -> Int {
if let dataArray = listOfScoresInfoArray [section]["data"] as? [Any] {
return dataArray.count
}
else
{
return 0
}
}
我想获取data计数信息需要显示numberOfRowsInSection
我已经搜索过与此相关的内容,但找不到正确的解决方案。任何人都建议我这样做。
listOfScoresInfoArray 输出就像 bwlow,
[["Date": "2017-06-14", "Data": [["lsPredictionPtsForWinOrDraw": "W : 16/X : 20", "points": 16.0, "finalScore": "2-1", "lsTeamName": "Russia", "matchStatus": World_Cup.WCPConstants.matchStatus.matchStatusEnd, "predictionResults": World_Cup.WCPConstants.predictionResults.predictionResultsWin, "rsPredictionPtsForWinOrDraw": "W : 25/X : 20", "rsTeamName": "S.Arabia", "startTime": "", "predictionScore": "2-1"]]], ["Date": "2017-06-15", "Data": [["lsPredictionPtsForWinOrDraw": "W : 30/X : 25", "points": 0.0, "finalScore": "1-1", "lsTeamName": "Egypt", "matchStatus": World_Cup.WCPConstants.matchStatus.matchStatusLive, "predictionResults": World_Cup.WCPConstants.predictionResults.predictionResultsNone, "rsPredictionPtsForWinOrDraw": "W : 16/X : 25", "rsTeamName": "Uruguay", "startTime": "18:00 - TF1", "predictionScore": "1-3"], ["lsPredictionPtsForWinOrDraw": "W : 20/X : 20", "points": 16.0, "finalScore": "1-1", "lsTeamName": "Marocco", "matchStatus": World_Cup.WCPConstants.matchStatus.matchStatusEnd, "predictionResults": World_Cup.WCPConstants.predictionResults.predictionResultsDraw, "rsPredictionPtsForWinOrDraw": "W : 22/X : 20", "rsTeamName": "Iran", "startTime": "", "predictionScore": "2-2"], ["lsPredictionPtsForWinOrDraw": "W : 30/X : 12", "points": 10.0, "finalScore": "1-1", "lsTeamName": "Portugal", "matchStatus": World_Cup.WCPConstants.matchStatus.matchStatusEnd, "predictionResults": World_Cup.WCPConstants.predictionResults.predictionResultsDraw, "rsPredictionPtsForWinOrDraw": "W : 14/X : 12", "rsTeamName": "Spain", "startTime": "", "predictionScore": "2-2"], ["lsPredictionPtsForWinOrDraw": "W : 10/X : 28", "points": 0.0, "finalScore": "1-1", "lsTeamName": "France", "matchStatus": World_Cup.WCPConstants.matchStatus.matchStatusEnd, "predictionResults": World_Cup.WCPConstants.predictionResults.predictionResultsLost, "rsPredictionPtsForWinOrDraw": "W : 34/X : 28", "rsTeamName": "Australia", "startTime": "", "predictionScore": "1-3"]]]]
【问题讨论】:
-
强烈建议使用自定义结构而不是字典。这就是 Swift:没有尾随分号。
标签: ios swift uitableview