【问题标题】:Best way to perform mutually exclusive selection in a tableview in different sections in swift 3在swift 3中不同部分的tableview中执行互斥选择的最佳方法
【发布时间】:2017-07-06 11:32:06
【问题描述】:

我有一个包含不同部分的表格视图,我需要能够从不同部分中进行多选,但每个部分中的行应该能够明智地选择互斥。例如:在下面的屏幕截图中,我应该能够从披萨中选择玛格丽塔或烧烤鸡,对于深盘披萨也是如此,但我应该能够在披萨部分和深盘披萨之间进行多选

下面是我目前的代码,我想知道解决这个问题的最佳方法是什么。

   let section = ["Pizza", "Deep dish pizza"]

    let items = [["Margarita", "BBQ Chicken"], ["Sausage", "meat lovers"]]

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.section[section]
    }



    override func numberOfSections(in tableView: UITableView) -> Int {

        return section.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return items[section].count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)

        // Configure the cell...

        cell.textLabel?.text = items[indexPath.section][indexPath.row]

        return cell
    }

【问题讨论】:

  • 你确定 tableview 是合适的选择吗? picker 听起来是个更好的选择

标签: ios uitableview swift3 multi-select xcode8


【解决方案1】:

您应该创建一些数据元素来跟踪每一行的选择。

我建议使用[Int:Int] 的字典,其中键是节,值是行。

选择一行后,您可以轻松检查该部分中是否已选择另一行,并在需要时取消选择。

var rowSelections = [Int:Int]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let section = indexPath.section
        if let row = self.rowSelections[section] {
            tableView.deselectRow(at: IndexPath(row:row, section:section), animated: true)
        }
        self.rowSelections[section]=indexPath.row
    }

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let section = indexPath.section
    self.rowSelections[section]=nil
}

【讨论】:

    【解决方案2】:

    好的,所以我想通了,我创建了一个方法,可以循环并检查所有行,并在 tableview didselect 和 deselect 中调用它

    func updateTableViewSelections(selectedIndex:IndexPath)
        {
    
            for  i in 0  ..< tableView.numberOfSections
            {
                for k in 0  ..< tableView.numberOfRows(inSection: i)
                {
    
                    if let cell = tableView.cellForRow(at: IndexPath(row: k, section: i))
    
                    {
    
                        if sections.getType(index: i) == selectedIndex.section
                        {
                            if (selectedIndex.row == k && cell.isSelected)
                            {
                                cell.setSelected(cell.isSelected, animated: false)
                            }
                            else
                            {
                                cell.setSelected(false, animated: false)
                            }
                        }
                    }
    
    
                }
            }
    
    
        }
    

    【讨论】:

    • 一旦表格视图有足够的单元格滚动,任何使用cellForRow(at:) 的解决方案都将不起作用,因为cellForRow(at:) 只能返回屏幕上的行。您不能依赖代表状态的表格视图/单元格;您需要在某处跟踪模型中的状态,以便在返回单元格时可以使用它
    • 谢谢,没想到。
    【解决方案3】:

    首先允许多选:

    yourTableView.allowsMultipleSelection = true
    

    获取选择行:

    let selectedRows = tableView.indexPathsForSelectedRows
    

    然后在 didselectrow 函数中,您可以遍历选定的行并确保只能选择该部分中的 1 行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多