快速选择表格视图中的单单元格 [英] Single cell Selection in table view in swift

查看:45
本文介绍了快速选择表格视图中的单单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中有两个部分.两个部分都在其下包含 3 个单元格.现在,当我选择任何单元格时,它会显示勾号,但它会从两个部分中选择一个单元格.我尝试了一些代码,但它不起作用.我希望用户可以从两个部分中选择一个单元格,并且在表视图加载其第一个单元格时应该预先选择.我怎样才能做到这一点?我对单元格的预选有点困惑.我的单元格选择代码是这样的,

i have a table view in which i have two sections. Both sections contain 3 cells under it. Now when i select any cell it shows tick sign, but it selects one cell from both section. i have tried some code but it isn't working. I want that user can select a single cell from both sections and when the table view load its first cell should be preselected. How can i do that? i'm bit confused about preselection of cell.My code for cell selection is this,

 self.filterTableView.allowsSelection = true
extension FiltersVC: UITableViewDelegate,UITableViewDataSource{

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuItems[section].count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = filterTableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FiltersTableViewCell

    cell.tilteLbl.text = menuItems[indexPath.section][indexPath.row]
    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.accessoryType = cell.isSelected ? .checkmark : .none
    cell.selectionStyle = .none
    cell.backgroundColor = UIColor.clear
    return cell
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()
    headerView.backgroundColor = #colorLiteral(red: 0.9130856497, green: 0.9221261017, blue: 0.9221261017, alpha: 1)

    let headerText = UILabel()
    headerText.textColor = UIColor.black
    headerText.adjustsFontSizeToFitWidth = true
    switch section{
    case 0:
        headerText.textAlignment = .center
        headerText.text = "LIST BY"
        headerText.backgroundColor = #colorLiteral(red: 0.9190355449, green: 0.9281349067, blue: 0.9281349067, alpha: 1)
        headerText.font = UIFont.boldSystemFont(ofSize: 20)
    case 1:
        headerText.textAlignment = .center
        headerText.text = "COUSINE"
        headerText.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
        headerText.font = UIFont.boldSystemFont(ofSize: 20)
    default: break

    }

    return headerText

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        if let cell = filterTableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark

            let item = menuItems[indexPath.section][indexPath.row]
            UserDefaults.standard.set(item, forKey: "listBy")
            UserDefaults.standard.synchronize()
            filterBtn.isUserInteractionEnabled = true
            filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
        }
    }
    else if indexPath.section == 1{
        if let cell = filterTableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark

            let item = menuItems[indexPath.section][indexPath.row]
            UserDefaults.standard.set(item, forKey: "Cuisine")
            UserDefaults.standard.synchronize()
            filterBtn.isUserInteractionEnabled = true
            filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
        }
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 {
        if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
        }
    }
    else if indexPath.section == 1{
        if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
        }
    }

}

推荐答案

您必须将选定的索引保存在某处,可能位于具有不同部分的某个数组中.由于您希望预先选择每个部分的第一个单元格,让我们从以下内容开始:

You have to save selected indexes somewhere, may be in some array with different sections. Since you want to have first cells of each section pre-selected lets start with something like this:

   var selectedIndexes = [[IndexPath.init(row: 0, section: 0)], [IndexPath.init(row: 0, section: 1)]]

在上面的数组中,我们保存了两个索引路径.一个用于第一部分的第一个单元格,第二个用于第二部分的第一个单元格.

In above array we are saving two indexpaths. One is for first cell of first section and the second is for first cell of second section.

现在你的 cellForRow 可以像这样检查数组中现有的索引路径:

Now your cellForRow may check for existing indexpaths in the array like so:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! UITableViewCell
    cell.selectionStyle = .none
    cell.textLabel?.text = tableArray[indexPath.section][indexPath.row]

    let selectedSectionIndexes = self.selectedIndexes[indexPath.section]
    if selectedSectionIndexes.contains(indexPath) {
        cell.accessoryType = .checkmark
    }
    else {
        cell.accessoryType = .none
    }


    return cell
}

单选:

// For single selection
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)

    // If current cell is not present in selectedIndexes
    if !self.selectedIndexes[indexPath.section].contains(indexPath) {
        // mark it checked
        cell?.accessoryType = .checkmark

        // Remove any previous selected indexpath
        self.selectedIndexes[indexPath.section].removeAll()

        // add currently selected indexpath
        self.selectedIndexes[indexPath.section].append(indexPath)

        tableView.reloadData()
    }
}

以上代码删除任何先前选择的单元格并保存新的单元格.如果同一个单元格被一次又一次地选中,它会保持选中状态.

Above code removes any previously selected cell and saves the new one. If the same cell is selected again and again it remains checked.

这篇关于快速选择表格视图中的单单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆