2017-06-05 11 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 

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:

私はどこから始めれば分からない

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. 

、そしてどのようなネストされたデータに対処する:

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 
    } 


} 

だから私はこれを見てみたいと思います。

ありがとうございました!

答えて

1

UITableViewは、本質的に複数のツリーレベルを記述するのではなく、エントリとヘッダーだけをサポートしています。

データモデルを3つのレベルから2つに平らにする(Date + League on top)。各セクションヘッダーには、日とリーグの両方が表示されます。

また、その日にヘッダーに日付を表​​示してから、その日のリーグ別にゲームを並べ替え、エントリセルのグループ化されたリーグをアイコンのインデントまたはセルの背景色で区別することもできます。

これが役に立ちます。

関連する問題