如何使用Array <dictionary <string,any>&gt;获取numberOfRowsInSection的计数在快速?

时间:2018-04-05 09:07:36

标签: ios swift uitableview

我无法使用类型广告Array<Dictionary<String,Any>>检索计数信息。现在我使用Swift 4.1启动了新项目。

我在UITableView(Grouped)上显示了带有节标题和行的静态数据。

我跟随此链接显示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"]]]]

1 个答案:

答案 0 :(得分:0)

您可以使用

获取当前部分
let section = listOfScoresInfoArray[section]

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

然后返回项目数

return rows.count

使用自定义结构更容易:

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
}