2017-06-05 90 views
0

我是一個應用程序的工作,我必須在TableView中顯示足球裝置。在UITableView中創建子部分(Swift 2.3)

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

UITableView本質上不支持您描述的多個樹級別,只是條目和標題。

我會將顯示的數據模型從三個級別展平爲兩個(Date + League頂部)。每個部分的標題將顯示當天和聯賽。

您也可以在標題中顯示一天,然後在當天按照聯盟排序遊戲,並使用圖標縮進或單元格背景顏色區分條目單元格中的分組聯賽。

希望這會有所幫助。