在UITableView中创建子部分(Swift 2.3)

时间:2017-06-05 15:53:03

标签: ios iphone swift uitableview

我当前正在开发应用程序,我必须在TableView中显示足球设备。

enter image description here

表格视图应如下所示(见上图) 作为树视图,它应该看起来像:

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.

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

UITableView本身并不支持您描述的多个树级别,只是条目和标题。

我会将数据模型从三个级别展平为两个级别(日期+联盟顶部)。每个部分标题都会显示当天和联盟。

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

希望这有帮助。