2016-04-22 4 views
0

私のコードの対応するインデックスを取得するためにsegmentedControlのselectedIndexのを使用することができます。どのように配列

class HomeViewController: UITableViewController { 
    var segmentedControl: HMSegmentedControl! 
    var text = [ 
     // 名词 
     ["音读名词","训读名词","音读与训读的分辨方法"], 
     // 形容词 
     ["如何分辨形容词", "常用形容词", "形容词的变形规则"] 
    ] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     configForSegmentedControl() 
    } 

    func configForSegmentedControl() { 
     segmentedControl = HMSegmentedControl(sectionTitles: ["名词","形容词","形容动词","片假名单词","动词","复合动词"]) 
     segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.width, 44) 
     segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10) 
     segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe 
     segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown 
     segmentedControl.addTarget(self, action: #selector(HomeViewController.segmentedControlChangedValue(_:)), forControlEvents: UIControlEvents.ValueChanged) 
     if let font = UIFont(name: "BigYoungMediumGB2.0", size: 15) { 
      segmentedControl.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: font] 
     } 
     segmentedControl.backgroundColor = UIColor.whiteColor() 
     segmentedControl.selectionIndicatorHeight = 3 
     segmentedControl.selectionIndicatorColor = UIColor.blackColor() 

     tableView.tableHeaderView = segmentedControl 
    } 

    func segmentedControlChangedValue(segmentedControl: HMSegmentedControl) { 
     print("Selected index \(segmentedControl.selectedSegmentIndex) (via UIControlEventValueChanged)") 
    } 
} 

extension HomeViewController { 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return text[segmentedControl.selectedSegmentIndex].count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     print("cellForRowAtIndexPath:\(segmentedControl.selectedSegmentIndex)") 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
     cell.textLabel?.text = text[segmentedControl.selectedSegmentIndex][indexPath.row] 
     return cell 
    } 
} 

そして、ここでは、コンソール出力です:

cellForRowAtIndexPath:0
にcellForRowAtIndexPath:0
cellForRowAtIndexPath:0
選択インデックス1(UIControlEventValueChanged経由)
選択インデックス0(

)UIControlEventValueChanged経由cellForRowAtIndexPathは、私はテキスト配列の要素を取得するための正しいインデックスを取得することができない理由であるsegmentedControlChangedValue前に呼び出されます。だから何をすべきか?コメントから、将来の参考のために

+0

コードを画像として送信しないでください。イメージを削除し、実際のコードをコピーして質問に貼り付けてください(正しくフォーマットするようにしてください)。また、ログ出力をコピーして画像として投稿するのではなく、質問に貼り付けてください。 – rmaddy

+0

もしcellForRowに値を入れたいのであれば、 'segmentedControlChangedValue'の中のtableviewをリロードしてください。 – HardikDG

+0

@Pyroありがとうございます! –

答えて

0

: あなたがcellForRowの値をしたい場合は、あなたは、選択/アクションに基づいてセルを設定する一般的なケースでは、その「segmentedControlChangedValue」

内部テーブルビューをあなたがすべきリロードする必要がありますreload/begin必要に応じてテーブルビューを更新する

関連する問題