我无法通过使用在堆栈溢出中搜索的复选标记将多个选定的行文本放入数组中,但无法实现它谁能帮助我如何获取数组中的文本?
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
}
}
}
这是图片
最佳答案
使用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/