简体   繁体   English

如何使用数组获取numberOfRowsInSection的计数 <Dictionary<String,Any> &gt;迅速?

[英]How to get count for numberOfRowsInSection using Array<Dictionary<String,Any>> in swift?

I can't retrieve count info using type cast Array<Dictionary<String,Any>> . 我无法使用类型转换Array<Dictionary<String,Any>>检索计数信息。 Right now I'm started new project using Swift 4.1. 现在,我开始使用Swift 4.1启动新项目。

And I displayed static data on UITableView (Grouped) with section headers and rows. 然后,我在带有部分标题和行的UITableView(分组)上显示了静态数据。

I followed to show the data post by GDSM from this link - How to form static data with the structure like array of objects in swift 4? 接下来,我展示了GDSM通过此链接发布的数据- 如何在 GDSM 4中使用类似于对象数组的结构来形成静态数据? (It's also I have asked before forming this kind of structures using NSMutableArray like in Objective c)

In ViewDidload I have assigned like var listOfScoresInfoArray = Array<Dictionary<String,Any>> () 在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
    }
}

I want to get data count info need to show numberOfRowsInSection 我想获取data计数信息,需要显示numberOfRowsInSection

I have searched related to this, but I can't get the right solution. 我已经搜索了与此相关的内容,但是找不到正确的解决方案。 Any one suggest me regarding this. 任何人都建议我这样做。

listOfScoresInfoArray output is like bwlow, 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"]]]]

You get the current section with 你得到当前部分

let section = listOfScoresInfoArray[section]

and the rows with 和行

let rows = section["Data"] as! Array<Dictionary<String,Any>>

then return the number of items 然后返回项目数

return rows.count

With custom structs it's much easier: 使用自定义结构可以轻松得多:

struct WCPConstants  { 
    enum MatchStatus { case end, live }
    enum PredictionResults { case none, win, draw, lost }
}

struct Match {
    let lsTeamName, rsTeamName : String
    let lsPredictionPtsForWinOrDraw, rsPredictionPtsForWinOrDraw : String
    let finalScore, predictionScore : String
    let points : CGFloat
    let startTime : String
    let matchStatus : WCPConstants.MatchStatus
    let predictionResults : WCPConstants.PredictionResults
}

struct Section {
    let date : String
    var matches = [Match]()
}

var sections = [Section]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.clear

    sections = prepareMatchInfo()
    print(sections)
}

func prepareMatchInfo() -> [Section] {

    var arrayOfMatches = [Section]()

    var arrayOfScoresSectionOne = [Match]()
    var arrayOfScoresSectionTwo = [Match]()

    arrayOfScoresSectionOne.append(Match(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: .end, predictionResults: .win))
    arrayOfScoresSectionTwo.append(Match(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: .live, predictionResults: .none))
    arrayOfScoresSectionTwo.append(Match(lsTeamName: "Marocco", rsTeamName: "Iran", lsPredictionPtsForWinOrDraw: "W : 20/X : 20", rsPredictionPtsForWinOrDraw: "W : 22/X : 20", finalScore: "1-1", predictionScore: "2-2", points: 16, startTime: "", matchStatus: .end, predictionResults: .draw))
    arrayOfScoresSectionTwo.append(Match(lsTeamName: "Portugal", rsTeamName: "Spain", lsPredictionPtsForWinOrDraw: "W : 30/X : 12", rsPredictionPtsForWinOrDraw: "W : 14/X : 12", finalScore: "1-1", predictionScore: "2-2", points: 10, startTime: "", matchStatus: .end, predictionResults: .draw))
    arrayOfScoresSectionTwo.append(Match(lsTeamName: "France", rsTeamName: "Australia", lsPredictionPtsForWinOrDraw: "W : 10/X : 28", rsPredictionPtsForWinOrDraw: "W : 34/X : 28", finalScore: "1-1", predictionScore: "1-3", points: 0, startTime: "", matchStatus: .end, predictionResults: .lost))
    arrayOfMatches.append(Section(date: "2017-06-14", matches: arrayOfScoresSectionOne))
    arrayOfMatches.append(Section(date: "2017-06-15", matches: arrayOfScoresSectionTwo))

    return arrayOfMatches
}

func getNumberOfRowsCountForSections(section: Int) -> Int {
   return sections[section].matches.count
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM