我目前正在开发一个应用程序,并且必须在TableView中显示足球器材。

ios - 在UITableView中创建子节(Swift 2.3)-LMLPHP

表格视图应如下所示(请参见上图)
作为树形视图,它应类似于:

Date
    League
        Fixture1
        Fixture2
    League2
        Fixture1
        Fixture2
Date
    League
        Fixture1
        Fixture2
    League2
        Fixture1
        Fixture2

所以我创建了一个DataManager来管理我的虚拟数据:
import Foundation
import RxSwift
import RxCocoa

class DataManager {
    var games = [Int:[League:[Game]]]()

    static let shared = DataManager()

    private init() {
        self.buildDummyData()
    }

    func buildDummyData() {

        let today = NSDate()
        let tomorrow = NSCalendar.currentCalendar()
            .dateByAddingUnit(
                .Day,
                value: 1,
                toDate: today,
                options: []
        )
        let day_after = NSCalendar.currentCalendar()
            .dateByAddingUnit(
                .Day,
                value: 2,
                toDate: today,
                options: []
        )

        let premierLeague = League(title: "Premier League", iName: "england")
        let ligueUn = League(title: "Ligue 1", iName: "France Contest")
        let europaLeague = League(title: "Europa League", iName: "Europa Contest")



        let game1 = Game(ftName: "Monaco", ftLogo: "sampleLogo", stName: "Paris SG", stLogo: "sampleLogo", time: "16:30", date: today, league: ligueUn)

        let game2 = Game(ftName: "Man U.", ftLogo: "sampleLogo", stName: "Liverpool", stLogo: "sampleLogo", time: "20:30", date: tomorrow!, league: premierLeague)

        let game3 = Game(ftName: "Celta Vigo", ftLogo: "sampleLogo", stName: "Man U.", stLogo: "sampleLogo", time: "19:45", date: day_after!, league: europaLeague)

        let game4 = Game(ftName: "ASSE", ftLogo: "sampleLogo", stName: "Man U.", stLogo: "sampleLogo", time: "19:45", date: day_after!, league: europaLeague)

        games = [0: [ligueUn: [game1]], 1: [premierLeague: [game2]], 2: [europaLeague: [game3, game4]]]


    }

}

还有我的ViewController.swift:
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    let dataManager = DataManager.shared

    let currentDate = NSDate()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.tableView.delegate = self
        self.tableView.dataSource = self

        tableView.registerNib(UINib(nibName: "GameTableViewCell", bundle: nil), forCellReuseIdentifier: "gameCell")



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataManager.games.count
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let view = UIView(frame: CGRectMake(0, 0, 350, 60))

        let label = UILabel(frame: CGRectMake(0, 13, 350, 33))

        view.addSubview(label)

        label.text = "Today"

        return view
    }

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60.0
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dataManager.games.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("gameCell", forIndexPath: indexPath) as! GameTableViewCell

//        var arrayOfLeagueAndGame = dataManager.games[indexPath.section]
//        var arrayOfGame = arrayOfLeagueAndGame?.popFirst()
//
//        cell.setupCell(arrayOfGame(indexPath.row))

        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50.0
    }


}

所以我想看看:
TODAY
   Ligue 1
        Monaco - PSG
TUESDAY 06 JUNE
    Premier League
        Man U. - Liverpool
WEDNESDAY 07 JUNE
    Europa League
        Celta Vigo - Man U.
        ASSE - Man U.

我不知道从哪里开始,以及如何处理此类嵌套数据。

谢谢你的帮助 !

最佳答案

UITableView本质上不支持您所描述的多个树级别,仅支持条目和标头。

我会将显示的数据模型从三个级别展平到两个级别(日期+联赛在顶部)。每个部分的标题将同时显示日期和联赛。

您也可以仅在标题中显示日期,然后按当天的联赛对游戏进行排序,并使用图标缩进或单元格背景色区分进入单元格中的分组联赛。

希望这可以帮助。

09-05 02:32