简体   繁体   中英

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>> . Right now I'm started new project using Swift 4.1.

And I displayed static data on UITableView (Grouped) with section headers and rows.

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? (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>> ()

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

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,

[["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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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