ios - 使用 UISplitViewController 的 Tableview 作为过滤器

标签 ios swift uitableview uisplitviewcontroller

我将 UISplitViewControllers 放在 UITabBarController 中。

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
}

现在,一旦我选择“所有帐户”单元格,然后我转到另一个选项卡“调用” 然后我回到“账户”选项卡,然后我选择“商业账户”,它被选中并且复选标记也在更新,但问题是“所有账户”单元格的复选标记没有消失。

最佳答案

此错误的发生是由于对 UITableViewUITableViewCell 进行了优化。这两个 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/

相关文章:

android - Cordova android地理定位超时

ios - 从 firebase 获取的数据有时不会添加到单元格中,尽管之前已经获取过

ios - 如何像stack swift 5一样在数组中一个接一个地添加/插入元素

iphone - 一行的大小不同于其他行的

ios - swift/iOS 8 : search bar raising fatal error: unexpectedly found nil while unwrapping an Optional value

ios - 如何测试 AFNetworking

android - 运行 FQL 查询时,通过适用于 Android/iOS 的 FB SDK 公开哪个版本的图形 API?

ios - 将 JSON 映射到模型,无论数组还是对象 - ObjectMapper

ios - 如何调整 UITextView 的大小,使其根据内容自动调整其宽度和高度,并在达到特定值时固定宽度?

ios - 获取当前行的正确编号(iOS)