2017-04-25 78 views
0

我在加載用戶排名的UIViewController中有一個UITableView。只有一個排名列表有點太有限,所以我添加了一個自定義欄來加載不同的排名(每週,每月和每年)。我之所以選擇這樣做,是因爲它給了我很多控制我的佈局約束 - 分段控制沒有。Swift - 使用自定義過濾器在tableView中返回數組數據

目前的問題是,我不完全知道如何根據我的菜單欄中選定的選項卡返回正確的數組。到目前爲止,我選擇了第四個空數組來複制其他三個數據之一的數據,但是如何將這些數據發回我的初始視圖,以便我可以在我的tableView的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 
} 
} 

更新代碼:

要預選的第一行,我還添加了這件T O我cellForItemAt indexPath

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

回答

2

建議您在菜單欄,上面寫着各種級別的選擇了什麼樣添加一個回調。您也可以使用此「Kind」來驅動菜單欄的顯示。

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暴露其模型(一個陣列),並且當該模型被複位重新加載其的tableview。

class RankingTableViewController: UITableViewController { 

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

上述代碼是爲了簡潔而存在的原始問題代碼的附加內容,即它不是獨立存在的。

+0

非常感謝!這應該做的伎倆。 –

+0

添加您的代碼:D它是完美的。你是我的英雄! –

+0

非常高興我可以幫助:D –

相關問題