ios - 如何从表格 View 中的选定单元格中获取文本?

标签 ios uitableview swift3

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

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
            }
        }
    }

这是图片

enter image description here

最佳答案

使用indexPathsForSelectedRows UITableView 的属性

您将能够获取所选行的所有 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 添加到 TableView 中。

关于ios - 如何从表格 View 中的选定单元格中获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46097546/

相关文章:

没有 Storyboard 的 iOS 用户界面 : Change UITextField font and guarantee minimum characters for a textField Object

swift - 在 Swift 中定义某些 iOS SDK 版本的范围

objective-c - 在界面构建器和模块化中使用 'external object'

objective-c - View 转换

ios - 基于屏幕尺寸的 "greater than or equal to"自动布局

ios - UITableView - 在索引路径问题处插入和删除行

ios - UITableViewCell 中的 NSLayoutConstraint 中断

ios - webtends - 如何测试它的工作

objective-c - 在表格 View 中显示特定大小的两个部分

ios - 每天在特定时间重置特定值