2016-06-01 59 views
1

我把UISplitViewControllers放在UITabBarController中。使用UISplitViewController的Tableview作为过滤器

enter image description here

我试图使用主视图作为过滤器。所以我用cellAccessoryType作为复选标记。只有一种可以选择。我写了这个代码是

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',你是否启用了'allowMultipleSelection'?你想启用它吗? – Kumuluzz

+0

已添加必需的密码@Kumuluzz 请看看。 不,我不想allowMultipleSelection。 即使我把它伪造同样的问题发生 –

回答

1

此错误是由于UITableViewUITableViewCell中已实施的优化而发生的。这两个视图非常高效,苹果使它们变得如此高效的一种方式是通过重用单元而不是一直实例化新单元(这就是为什么你要调用dequeueReusableCellWithIdentifier而不是每次都实例化一个新单元)。

为了克服这个错误,每次使用时都必须重置单元格。

这可以通过两种方式来完成:

  • 改写prepareForReuse如果你继承UITableViewCell(但是这是不是一种选择适合你,因为你使用的是标准UITableViewCell
  • 直接重置性质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 完全加载表视图后,我选择一个单元格。 所以现在它被选中,checkMark将在那里。然后切换选项卡并再次切换回来。现在,如果我尝试选择另一个单元格,它也将被选中,因为在切换回来时表格视图不会再次加载。 –

+0

我也试过了。该想法不起作用。我认为问题在于,当我们切换标签页时表格没有被重新加载。请让我知道你是否有新的想法。谢谢 –