2016-06-01 5 views
1

私はUISabitControllerをUITabBarControllerに配置しています。UISplitViewControllerのTableviewをフィルタとして使用する

enter image description here

私はフィルタとしてマスタービューを使用しようとしています。だから私はチェックマークとしてcellAccessoryTypeを使用しました。そのうち1つだけを選択できます。私はその後、私は、[通話] それから私カムバックタブ「アカウント」への別のタブに移動し、その後、私が選択し、「すべてのアカウント」のセルを選択すると、私はこのために書いたコードは、「今すぐビジネスアカウント

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.selectedIndex = indexPath.row 

    let cell:UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath)! 
    cell.accessoryType = .Checkmark 
    self.performSegueWithIdentifier("dispAccounts", sender: self) 
     } 
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell:UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath)! 
    cell.accessoryType = .None 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 
    filterList = ["All Accounts","Business Accounts","Person Accounts"] 
    self.tableView.allowsMultipleSelection = false 
    //self.splitViewController?.maximumPrimaryColumnWidth = 140; //This line is to restrict the width of master View of UISplitVC 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 3 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("accountCell", forIndexPath: indexPath) 

    cell.textLabel?.text = filterList[indexPath.row] 
    return cell 
} 

です'選択され、チェックマークも更新されていますが、問題は'すべてのアカウント 'のセルのチェックマークが消えていないことです。

+0

'cellForRowAtIndexPath'で使用するコードを表示できますか?また、あなたの 'UITableView'では' allowsMultipleSelection'を有効にしていますか?有効にしますか? – Kumuluzz

+0

必要なコードを追加しました@Kumuluzz ご覧ください。 いいえ.MultipleSelectionを許可しません。 私がそれを偽にしても、同じ問題が発生しています –

答えて

1

このバグは、UITableViewおよびUITableViewCellに実装されている最適化により発生します。これらの2つのビューは非常に効率的で、アップルが効率的に作った方法の1つは、常に新しいセルをインスタンス化するのではなく、セルを再利用することです(毎回新しいセルをインスタンス化するのではなくdequeueReusableCellWithIdentifierを呼び出しています)。

このバグを克服するためには、使用するたびにセルをリセットする必要があります。

これは、2つの方法で行うことができますで直接プロパティをリセット

  • あなたがUITableViewCellをサブクラス化された(ただし、標準UITableViewCellを使用しているので、これはあなたのためのオプションではない)場合

    • prepareForReuseを上書きcellForRowAtIndexPath

    だから、あなたのための可能な解決策は、次のようになります。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
        // Getting the cell 
        let cell = tableView.dequeueReusableCellWithIdentifier("accountCell", forIndexPath: indexPath) 
    
        // Resetting the cell 
        cell.textLabel?.text = "" 
        cell.selected = false 
    
        // Configuring the cell 
        cell.textLabel?.text = filterList[indexPath.row] 
    
        // Returning the finished cell 
        return cell 
    } 
    
  • +0

    良いアイデア@Kumuluzz –

    +0

    しかし@Kumuluzz 完全にテーブルビューを読み込んだら、セルを選択しています。 これで選択され、チェックマークがそこに表示されます。その後、タブを切り替えて再びスイッチバックします。今、別のセルを選択しようとすると、テーブルビューが切り替え時に再度読み込まれないため、選択されたセルも選択されます。 –

    +0

    私も試しました。アイデアは機能していません。私は、テーブルを切り替えるときにテーブルがリロードされないという問題があると思います。新しいアイデアが得られたら教えてください。ありがとう –

    関連する問題