2017-04-25 4 views
0

ユーザーのランキングを読み込むUIViewController内にUITableViewがあります。ランク付けリストが1つしかないのは少し制限されていたので、私はカスタムバーを追加して別のランキング(毎週、毎月、毎年)を読み込んでいます。私がこのようにする理由は、レイアウトの制約を私に多く与えてくれたからです。セグメント化されたコントロールはそうではありません。Swift - カスタムフィルタ付きのtableViewで配列データを返す

現在の問題は、メニューバーの選択したタブに基づいて適切な配列を返す方法が正確にわからないことです。今のところ、私は4番目の空の配列を使用して、タブが選択されているときに他の3つのデータの1つをコピーしますが、私のテーブルビューのnumberOfRowsInSectionでカウントを返すことができるように、コード更新

のViewController & TableViewController

class Rank: NSObject{ 
    var name: String 
    var points: Int 

init(name: String, points: Int) { 
     self.name = name 
     self.points = points 
    } 
} 

var rankingArrayWeek = [Rank]() 
var rankingArrayMonth = [Rank]() 
var rankingArrayTotal = [Rank]() 
var filteredRanking = [Rank]() 

class RankingController: UIViewController, UITableViewDelegate { 

weak var tableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    setupMenuBar() 

    tableView?.delegate = self 

    rankingArrayWeek = [ 
     Rank(name: "Name1", points: 200) 
    ] 

    rankingArrayMonth = [ 
     Rank(name: "Name1", points: 300), 
     Rank(name: "Name2", points: 200), 
    ] 

    rankingArrayTotal = [ 
     Rank(name: "Name1", points: 500), 
     Rank(name: "Name2", points: 400), 
     Rank(name: "Name3", points: 300), 
    ] 

    let rankingTableViewController = RankingTableViewController() 

    self.addChildViewController(rankingTableViewController) 
    rankingTableViewController.view.translatesAutoresizingMaskIntoConstraints = false 

    view.addSubview(rankingTableViewController.view) 

    rankingTableViewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true 
    rankingTableViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 10).isActive = true 
    rankingTableViewController.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    rankingTableViewController.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true 
} 

lazy var menuBar: MenuBar = { 
    let menuBar = MenuBar() 
    menuBar.rankingController = self 
    return menuBar 
}() 

private func setupMenuBar() { 
    navigationController?.hidesBarsOnSwipe = true 

    view.addSubview(menuBar) 
    view.addConstraintsWithFormat("H:|[v0]|", views: menuBar) 
    view.addConstraintsWithFormat("V:|[v0(50)]", views: menuBar) 
    } 
} 

// MARK: TableViewController 
class RankingTableViewController: UITableViewController { 

    let cellId = "cellId" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView?.register(RankCell.self, forCellReuseIdentifier: cellId) 
     tableView?.tableFooterView = UIView(frame: CGRect.zero) 
     tableView?.rowHeight = 60 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return filteredRanking.count 
    } 
} 

マイカスタムメニューバー

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    collectionView.isScrollEnabled = false 
    collectionView.backgroundColor = .white 
    collectionView.dataSource = self 
    collectionView.delegate = self 
    return collectionView 
}() 

let cellId = "cellId" 
let names = ["Week", "Month", "Total"] 

var rankingController: RankingController? 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId) 

    addSubview(collectionView) 
    addConstraintsWithFormat("H:|[v0]|", views: collectionView) 
    addConstraintsWithFormat("V:|[v0]|", views: collectionView) 

    let selectedIndexPath = NSIndexPath(item: 2, section: 0) 
    collectionView.selectItem(at: selectedIndexPath as IndexPath, animated: false, scrollPosition: .centeredHorizontally) 

    setupHorizontalBar() 
} 

var horizontalBarLeftAnchorConstraint: NSLayoutConstraint? 

func setupHorizontalBar() { 
    let horizontalBarView = UIView() 
    horizontalBarView.backgroundColor = Constants.MAIN_THEME_COLOR 
    horizontalBarView.translatesAutoresizingMaskIntoConstraints = false 
    addSubview(horizontalBarView) 

    horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor) 
    horizontalBarLeftAnchorConstraint?.isActive = true 
    horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 
    horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/3).isActive = true 
    horizontalBarView.heightAnchor.constraint(equalToConstant: 4).isActive = true 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let x = CGFloat(indexPath.item) * frame.width/3 
    horizontalBarLeftAnchorConstraint?.constant = x 

    UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { self.layoutIfNeeded() 
     }, completion: nil) 

    if indexPath.item == 0 { 
     filteredRanking = rankingArrayWeek 
     print(filteredRanking.count) 
    } else if indexPath.item == 1 { 
     filteredRanking = rankingArrayMonth 
     print(filteredRanking.count) 
    } else { 
     filteredRanking = rankingArrayTotal 
     print(filteredRanking.count) 
    } 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell 

    cell.buttonView.text = "\(names[indexPath.item])" 
    cell.buttonView.textColor = Constants.MAIN_THEME_COLOR 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: frame.width/3, height: frame.height) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
} 
} 

:最初の行を事前に選択するには

を、私はまた、このトンを追加しましたO私のcellForItemAt indexPath

if (cell.isSelected == false) { 
     didSelect?(kinds[0]) 
    } 

答えて

2

はあなたのランクの種類が選択されていると言うあなたのメニューバーにコールバックを追加することを提案します。この「種類」を使用して、メニューバーの表示を駆動することもできます。

enum RankKind: String { 
    case week = "Week" 
    case month = "Month" 
    case total = "Total" 
} 

class MenuBar { 

    let kinds = [RankKind.week, .month, .total] 

    var didSelect: ((RankKind)->())? 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return kinds.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     cell.buttonView.text = kinds[indexPath.item].rawValue) 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     didSelect?(kinds[indexPath.item]) 
    } 
} 

この場合、RankingControllerは、メニューの種類がいつ変更されたかを知るように設定できます。

class RankingController { 

    func viewDidLoad() { 

     menuBar.didSelect = { kind in 
      rankingTableViewController.rankingArray = self.rankingArrayFor(kind: kind) 
     } 

    } 

    func rankingArrayFor(kind: RankKind) -> [Rank] { 
     switch kind { 
     case .week: return rankingArrayWeek 
     case .month: return rankingArrayMonth 
     case .total:return rankingArrayTotal 
     } 
    } 
} 

最後に、RankingTableViewControllerは、そのモデル(アレイ)を公開し、そのモデルがリセットされたときに、そのテーブルビューをリロード。

class RankingTableViewController: UITableViewController { 

    var rankingArray: [Rank] = [] { 
     didSet { 
      self.tableView.reloadData() 
     } 
    } 
} 

上記のコードは、簡潔さのために既存の質問のコードに追加されています。つまり、それは独立したものではありません。

+0

非常に感謝しています!これはトリックを行う必要があります。 –

+0

あなたのコードを追加しました:D完璧です。あなたは私のヒーローです! –

+0

私は助けてくれるととてもうれしく思っています:D –

関連する問題