我将 UISplitViewControllers 放在 UITabBarController 中。
我正在尝试使用主视图作为过滤器。所以我使用 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
}
现在,一旦我选择“所有帐户”单元格,然后我转到另一个选项卡“调用” 然后我回到“账户”选项卡,然后我选择“商业账户”,它被选中并且复选标记也在更新,但问题是“所有账户”单元格的复选标记没有消失。
最佳答案
此错误的发生是由于对 UITableView
和 UITableViewCell
进行了优化。这两个 View 非常高效,Apple 使它们如此高效的一种方法是重用单元而不是一直实例化新单元(这就是为什么你调用 dequeueReusableCellWithIdentifier
而不是每次都实例化一个新单元时间)。
为了克服这个错误,每次使用时都必须重置单元格。
这可以通过两种方式完成:
- 如果您正在对
UITableViewCell
进行子类化,则覆盖prepareForReuse
(但这不适合您,因为您使用的是标准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
}
关于ios - 使用 UISplitViewController 的 Tableview 作为过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37561675/