【问题标题】:How to get the text from the selected cells in a table view?如何从表格视图中的选定单元格中获取文本?
【发布时间】:2017-09-07 13:23:01
【问题描述】:

我无法通过使用在堆栈溢出中搜索的复选标记将多个选定的行文本放入数组中,但无法实现它,谁能帮助我如何获取数组中的文本?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return productName.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "filterSelectionCell", for: indexPath) as! FilterSelectionCell
        activityIndicator.stopAnimating()
        activityIndicator.hidesWhenStopped = true
        tableDetails.isHidden = false
        cell.brandProductName.text = productName[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            if cell.accessoryType == .checkmark{
                cell.accessoryType = .none
            }
            else{
                cell.accessoryType = .checkmark
            }
        }
    }

这是图片

【问题讨论】:

  • 从数据源(model)而不是从单元格(view)获取数据。将selected 属性添加到模型并相应地在cellForRow 中设置附件类型。要获取所有选定的单元格,只需按 selected == true 过滤数据源数组。

标签: ios uitableview swift3


【解决方案1】:

使用 UITableView 的indexPathsForSelectedRows 属性

您将能够获取所选行的所有 indexPaths,然后整合这些 indexPaths 的数组并从您的数据集中获取您的文本(在您的情况下为 productName 数组)。

像这样:

fun getAllTextFromTableView() {
  guard let indexPaths = self.tableView.indexPathsForSelectedRows else { // if no selected cells just return
    return
  }

  for indexPath in indexPaths {
    print("\(productName[indexPath.row])") //Here you get the text of cell
  }
}

当然,您需要在表格视图中添加@IBOutlet 才能在函数中访问它。

【讨论】:

  • 我需要获取数组中的元素如何获取数组? @Sergey Pekar
  • 不需要,但如果我取消选中如何从数组中删除它? @Sergey Pekar
  • 最后我把它删除了也谢谢兄弟@Sergey Pekar
【解决方案2】:

您应该为表的数据源使用产品对象数组而不是名称数组。每个产品都有一个名称和一个 Bool 值来说明它是否被选中。

然后,查找选定的产品将变得很容易,而且更重要的是,您不会使用 UI 作为数据模型来了解是否选择了某些产品。

【讨论】:

    【解决方案3】:

    这是我对以下代码的回答,您可以从之前附加到数组中的数组中删除选定的字符串

    var values = [String]()
    var selected: Bool
    
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
            selected = false
            if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
                if cell.accessoryType == .checkmark{
                    cell.accessoryType = .none
                    print("\(productName[indexPath.row])")
                    values = values.filter{$0 != "\(productName[indexPath.row])"}
                    selected = true
                    print(values)
                }
                else{
                    cell.accessoryType = .checkmark
                }
            }
            if selected == true{
                print(values)
            }
            else{
                getAllTextFromTableView()
            }
    
        }
        func getAllTextFromTableView() {
        guard let indexPaths = self.tableDetails.indexPathsForSelectedRows else { // if no selected cells just return
        return
        }
    
        for indexPath in indexPaths {
            values.append(productName[indexPath.row])
            print(values)
        }
        }
    

    【讨论】:

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