在swift中的表视图中选择单个单元格

时间:2018-05-21 03:16:04

标签: swift tableview

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

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

}

4 个答案:

答案 0 :(得分:2)

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

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

在上面的数组中,我们保存了两个索引路径。一个是第一部分的第一个细胞,第二个是第二部分的第一个细胞。

现在你的cellForRow可能会检查数组中的现有索引路径,如下所示:

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

上面的代码删除任何先前选择的单元格并保存新单元格。如果一次又一次选择相同的单元格,则会保持检查状态。

答案 1 :(得分:1)

您可以尝试以下方法来保持一次选择一个单元格

**My arrays**

let section = ["pizza", "deep dish pizza", "calzone"]

let items = [["Margarita", "BBQ Chicken", "Peproni", "BBQ Chicken", "Peproni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns & mashrooms"]]

/// Lets keep index Reference for which cell is 
/// Getting selected
var selectedIndex : [Int:Int]?

/// Now in didLoad
override func viewDidLoad() {
   super.viewDidLoad()
   /// Initialise the Dictionary 
   selectedIndex = [Int:Int]()

   /// Set the Default value as in your case
   /// Section - 0 and IndexPath - 0
   /// i.e First cell
   selectedIndex?.updateValue(0, forKey: 0)
   mainTableView.delegate = self
   mainTableView.dataSource = self
   mainTableView.reloadData()
} 

func numberOfSections(in tableView: UITableView) -> Int {
   return section.count
}

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

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

    /// my Header cell
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! TableViewCell
    headerCell.titleLabel.text = self.section[section]
    headerCell.ButtonToShowHide.tag = section
    return headerCell.contentView

}

/// Now in CellForRowMethod

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

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

        /// Compare if current section is Available in reference Dict
        if let val = selectedIndex![indexPath.section]{
            /// If Yes
            /// Check IndexPath Row Value
            if indexPath.row == val{
                /// If row is found that is selected
                /// Make it highlight
                /// You can set A radio button
                cell.textLabel?.textColor = UIColor.red
            }
            else{
                /// Set default value for that section
                cell.textLabel?.textColor = UIColor.black
            }
        }
        /// If no
        else{
            /// Required to set Default value for all other section
            /// And
            /// Required to Update previous value if indexPath was selected
            /// In previouus index Section
            cell.textLabel?.textColor = UIColor.black
        }

        return cell
    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        /// Remove All values
        selectedIndex?.removeAll()
        /// Insert current Value
        selectedIndex?.updateValue(indexPath.row, forKey: indexPath.section)
        /// Reload TableView Fully
        /// Or you can keep Reference of Previous and New Value
        /// For not reloading All cells
        self.mainTableView.reloadData()
    }

<强>输出

加载TableView时

enter image description here

在同一部分中选择行

enter image description here

在其他部分选择行时

enter image description here

GIF工作 - 更新单元格标签颜色

enter image description here

<强>重新Upddate

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

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

        /// Compare if current section is Available in reference Dict
        if let val = selectedIndex![indexPath.section]{
            /// If Yes
            /// Check IndexPath Row Value
            if indexPath.row == val{
                /// If row is found that is selected
                /// Make it highlight
                /// You can set A radio button
                cell.accessoryType = .checkmark
            }
            else{
                /// Set default value for that section
                cell.accessoryType = .none
            }
        }
        /// If no
        else{
            /// Required to set Default value for all other section
            /// And
            /// Required to Update previous value if indexPath was selected
            /// In previouus index Section
            cell.accessoryType = .none
        }

        return cell
    }

答案 2 :(得分:1)

  • 启用allowsMultipleSelection一次选择多个单元格。
  • 使用tableView(_:willSelectRowAt:)来处理选择。如果| indexPath.section |中有选定的单元格,请取消选择此行

    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
      let indexPathsForSelectedRows = tableView.indexPathsForSelectedRows ?? [IndexPath]()
    
      // If there is a selected cell in |indexPath.section|, do nothing
      for selectedIndexPath in indexPathsForSelectedRows {
        if selectedIndexPath.section == indexPath.section {
          tableView.deselectRow(at: selectedIndexPath, animated: true)
        }
      }
    
      return indexPath;
    }
    

答案 3 :(得分:0)

indexPaths是类型为IndexPath的本地数组变量,以防万一您感到困惑。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.deselectRow(at: indexPath, animated: true)

    if indexPaths.count > 0 {
        tableView.cellForRow(at: indexPaths[0])?.accessoryType = .none

        if indexPaths[0] == indexPath {
            tableView.cellForRow(at: indexPath)?.accessoryType = .none
            self.indexPaths.removeAll()
            return
        }

        self.indexPaths.removeAll()
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        self.indexPaths.append(indexPath)
    } else {
        self.indexPaths.append(indexPath)
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
}